前端获取当前的时间

1. data里面可以定义一个参数

data() {
    
    
        return {
    
    
        	 nowDateTime: '',//当前日期时间
        }
    },

2.在methods里面定义一个方法进行时间的获取的实现

	getTime(){
    
    
        var _this = this;
        let yy = new Date().getFullYear();
        let mm = new Date().getMonth()+1;
        let dd = new Date().getDate();
        let hh = new Date().getHours();
        let mf = new Date().getMinutes()<10 ? '0'+new Date().getMinutes() : new Date().getMinutes();
        let ss = new Date().getSeconds()<10 ? '0'+new Date().getSeconds() : new Date().getSeconds();
        _this.nowDateTime = yy+'年 '+mm+'月'+dd+'日 '+hh+':'+mf+':'+ss;
      },
      currentTime(){
    
    
      setInterval(this.getTime,1000)  	
    	},

3.在需要进行显示的地方进行显示当前的时间

<span>{
   
   {nowDateTime}}</span>

猜你喜欢

转载自blog.csdn.net/houzhicongone/article/details/121818900