获取当前时间
onLoad: function(){
var that = this;
that.getNowTime()
}
getNowTime: function() {
var now = new Date();
var year = now.getFullYear();
var month = now.getMonth() + 1;
var day = now.getDate();
if(month < 10) {
month = '0' + month;
};
if(day < 10) {
day = '0' + day;
};
// 如果需要时分秒,就放开
var h = now.getHours();
var m = now.getMinutes();
var s = now.getSeconds();
var formatDate = year + '-' + month + '-' + day + ' ' + h + ':' + m + ':' + s;
console.log('当前时间',formatDate)
return formatDate;
},
获取昨天、今天、明天
today 是需要计算的某一天的日期例如“2018-12-12”,传 null 默认今天,addDayCount 是要推算的天数, -1是前一天,0是今天,1是后一天。
onLoad: function(options) {
var that = this;
//获取明天
that.getDateStr(null,1)
},
getDateStr: function(today, addDayCount) {
var date;
if(today) {
date = new Date(today);
}else{
date = new Date();
}
date.setDate(date.getDate() + addDayCount);//获取AddDayCount天后的日期
var y = date.getFullYear();
var m = date.getMonth() + 1;//获取当前月份的日期
var d = date.getDate();
if(m < 10){
m = '0' + m;
};
if(d < 10) {
d = '0' + d;
};
console.log( y + "-" + m + "-" + d)
return y + "-" + m + "-" + d;
},
原文:https://www.jianshu.com/p/9645cd3461d3
网友评论