中国标准时间转换为特定格式时间(以2020-006-06为例)
initDate(date){
return date.getFullYear() + "-" + (date.getMonth()> 9 ? (date.getMonth() + 1) : "0" + (date.getMonth() + 1)) + "-" +(date.getDate()> 9 ? (date.getDate()) : "0" + (date.getDate()));
},
本周、上周的开始日期和结束日期
getTime(n) {
let now = new Date();
let year = now.getFullYear();
let month = now.getMonth() + 1;
let day = now.getDay(); //返回星期几的某一天;
n = day == 0 ? n + 6 : n + (day - 1)
now.setDate(now.getDate() - n);
let date = now.getDate();
let s = year + "-" + (month < 10 ? ('0' + month) : month) + "-" + (date < 10 ? ('0' + date) : date);
return s;
},
/***参数都是以周一为基准的***/
//上周的开始时间
console.log(this.getTime(7));
//上周的结束时间
console.log(this.getTime(1));
//本周的开始时间
console.log(this.getTime(0));
//本周的结束时间
console.log(this.getTime(-6));
//下周的开始时间
console.log(this.getTime(-7))
//下周的结束时间
console.log(this.getTime(-13))
上月开始时间和结束时间
lastMonth(){
let nowday = new Date();
let year = nowday.getFullYear();
let month = nowday.getMonth();
if(month==0){
month=12;
year=year-1;
}
if (month < 10) {
month = "0" + month;
}
let firstDayOfPreMonth = year + "-" + month + "-" + "01";
let lastDay = new Date(year, month, 0);
let lastDayOfPreMonth = year + "-" + month + "-" + lastDay.getDate();
firstDayOfPreMonth = firstDayOfPreMonth.toString();
lastDayOfPreMonth = lastDayOfPreMonth.toString();
let value4 = [];
value4.push(firstDayOfPreMonth); //开始时间
value4.push(lastDayOfPreMonth); //结束时间
return value4
},
// 输出结果为 ["2020-05-01", "2020-05-31"]
本月开始日期、结束日期
CurrentMonthLast(){
let date=new Date();
let currentMonth=date.getMonth();
let nextMonth=++currentMonth;
let nextMonthFirstDay=new Date(date.getFullYear(),nextMonth,1);
let oneDay=1000*60*60*24;
let start = this.initDate(new Date(new Date().setDate(1)))
let end = this.initDate(new Date(nextMonthFirstDay-oneDay))
return [start,end]
},
//输出结果 ["2020-06-01", "2020-06-30"]