【时间】获取当前时间 年月日时分秒


	d = d.replace(/-/g,'/')
	
// 获取当前时间 年月日时分秒
			getNowDate(time){
			  var date = new Date();
			  var sign2 = ":";
			  var year = date.getFullYear() // 年
			  var month = date.getMonth() + 1; // 月
			  var day = date.getDate(); // 日
			  
			  var hours = date.getHours(), // 小时
			  var minutes = date.getMinutes(), // 分
			  var seconds = date.getSeconds(), // 秒
			  var milliseconds= date.getMilliseconds(), // 毫秒
			  
			  // 给一位数的数据前面加 “0”
			  if (month >= 1 && month <= 9) {
			    month = "0" + month;
			  }
			  if (day >= 0 && day <= 9) {
			    day = "0" + day;
			  }
			  if (hours >= 0 && hours <= 9) {
			    hours = "0" + hours;
			  }
			  if (minutes >= 0 && minutes <= 9) {
			    minutes  = "0" + minutes ;
			  }
			   if (seconds>= 0 && seconds<= 9) {
			    seconds= "0" + seconds;
			  }
				
				if(time == 'year'){
					 return  year + '-' + month + '-' +  day;
				}
				
				if(time == 'seconds'){
					 return  year + '-' + month + '-' +  day  + hours+':'+ minutes  +':'+seconds ;
				}
			},

调用:

// 获取当前时间
this.dangqianshijian = this.getNowDate('year'); // 默认当天日期  年月日

this.dangqianshijian = this.getNowDate('seconds'); // 默认当天日期  年月日 时分秒

你可能感兴趣的:(uni-app,javascript)