这是最近写的一个项目需要用到的考勤月和自然月,这种是最笨的判断性,后来上网找了一些时间的格式化,
var d = new Date()
this.nowdatarq = (d.getDate());
if (this.nowdatarq < 21) {
if (d.getMonth() + 1 < 10) {
this.rqDate = d.getFullYear() + '-' + ('0' + (d.getMonth() + 1))
}
else {
this.rqDate = d.getFullYear() + '-' + (d.getMonth() + 1)
}
}
else {
if (d.getMonth() + 2 < 10) {
this.rqDate = d.getFullYear() + '-' + ('0' + (d.getMonth() + 2))
}
else if (d.getMonth() + 2 <= 12) {
this.rqDate = d.getFullYear() + '-' + (d.getMonth() + 2)
}
else if (d.getMonth() + 2 > 12) {
this.rqDate = (d.getFullYear() + 1) + '-01'
}
}
format(time, format) {
var t = new Date(time);
var tf = function(i) {
return (i < 10 ? "0" : "") + i;
};
return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function(a) {
switch (a) {
case "yyyy":
return tf(t.getFullYear());
break;
case "MM":
return tf(t.getMonth() + 1);
break;
case "mm":
return tf(t.getMinutes());
break;
case "dd":
return tf(t.getDate());
break;
case "HH":
return tf(t.getHours());
break;
case "ss":
return tf(t.getSeconds());
break;
}
});
},
var d = new Date()
var data=this.format(d, "yyyy-MM-dd");
js 获取当前日期周一和周日
var now = new Date();
var nowTime = now.getTime() ;
var day = now.getDay();
var oneDayTime = 24*60*60*1000 ;
var MondayTime = nowTime - (day-1)*oneDayTime ;//显示周一
var SundayTime = nowTime + (7-day)*oneDayTime ;//显示周日
console.log(new Date(MondayTime));
console.log(new Date(SundayTime))
js获取当前日期的当月第一天和最后一天
let val = = new Date();
var year = val.getFullYear();
var month = val.getMonth() + 1;
let firstdate = year + '-' + month + '-01';//当月第一天
var day = new Date(year, month, 0);
let lastdate = year + '-' + month + '-' + day.getDate();//当月最后一天
console.log(firstdate, "firstdate")
console.log(lastdate, "lastdate")