日期函数(最近一周/一个月/半年/一年)
// getTime 返回 1970 年 1 月 1 日至今的毫秒数。
// setTime(毫秒) 方法以毫秒设置 Date 对象。
//今天
function today(){
const newDay = new Date();
return newDay.getFullYear()+"-" + (newDay.getMonth()+1) + "-" + newDay.getDate()
}
//最近一周
function lastWeek(){
const end = new Date();
const start = new Date();
start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
return [start.getFullYear()+"-" + (start.getMonth()+1) + "-" + start.getDate(),end.getFullYear()+"-" + (end.getMonth()+1) + "-" + end.getDate()]
}
//最近一个月
function lastMonth(){
const end = new Date();
const start = new Date();
start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
return [start.getFullYear()+"-" + (start.getMonth()+1) + "-" + start.getDate(),end.getFullYear()+"-" + (end.getMonth()+1) + "-" + end.getDate()]
}
//最近三个月
function lastThreeMonths(){
const end = new Date();
const start = new Date();
start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
return [start.getFullYear()+"-" + (start.getMonth()+1) + "-" + start.getDate(),end.getFullYear()+"-" + (end.getMonth()+1) + "-" + end.getDate()]
}
//最近半年
function lastSixMonths(){
const end = new Date();
const start = new Date();
start.setTime(start.getTime() - 3600 * 1000 * 24 * 182);
return [start.getFullYear()+"-" + (start.getMonth()+1) + "-" + start.getDate(),end.getFullYear()+"-" + (end.getMonth()+1) + "-" + end.getDate()]
}
//最近一年
function lastYear(){
const end = new Date();
const start = new Date();
start.setTime(start.getTime() - 3600 * 1000 * 24 * 365);
return [start.getFullYear()+"-" + (start.getMonth()+1) + "-" + start.getDate(),end.getFullYear()+"-" + (end.getMonth()+1) + "-" + end.getDate()]
}