js获取前三天,前七天,前一个月的时间

function getTime(){
var getNowDate = new Date();
//获取当前日期时间
const nowYear = getNowDate.getFullYear();
let nowMonth = getNowDate.getMonth()+1;
let nowDay = getNowDate.getDate();
if(nowMonth >= 1 && nowMonth <=9 ){
nowMonth = ‘0‘ + nowMonth;
}
if(nowDay >=1 && nowDay <=9 ){
nowDay = ‘0‘ + nowDay
}else if(nowDay == 1){
nowDay = 30;
nowMonth -= 1;
}
var nowDate = nowYear + ‘-‘ + nowMonth + ‘-‘ + nowDay;
//获取前三天时间
var getThreeDate = new Date(getNowDate -3*24*3600*1000);
var thYear = getThreeDate.getFullYear();
var thMonth = getThreeDate.getMonth()+1;
var thDay = getThreeDate.getDate();
if(thMonth >= 1 && thMonth <= 9 ){
thMonth = ‘0‘ + thMonth
}
if(thDay >= 0 && thDay <=9 ){
thDay = ‘0‘ + thDay
}
var thDate = thYear + ‘-‘ + thMonth + ‘-‘ + thDay;
//获取前七天时间
var getWeekDate = new Date(getNowDate -7*24*3600*1000);
var weekYear = getWeekDate.getFullYear();
var weekMonth = getWeekDate.getMonth()+1;
var weekDay = getWeekDate.getDate();
if(weekMonth >= 1 && weekMonth <=9 ){
weekMonth = ‘0‘ + weekMonth
}
if(weekDay >= 1 && weekDay <= 9){
weekDay = ‘0‘ + weekDay
}
var weekDate = weekYear + ‘-‘ + weekMonth + ‘-‘ + weekDay;
//获取一个月前时间
getNowDate.setMonth(getNowDate.getMonth()-1);
var thirtyYear = getNowDate.getFullYear();
var thirtyMonth = getNowDate.getMonth()+1;
var thirtyDay = getNowDate.getDate();
if(thirtyMonth >= 1 && thirtyMonth <=9 ){
thirtyMonth = ‘0‘ + thirtyMonth
}
if(thirtyDay >= 1 && thirtyDay <= 9){
thirtyDay = ‘0‘ + thirtyDay
}
var monthDate = thirtyYear + ‘-‘ + thirtyMonth + ‘-‘ + thirtyDay;
return {‘nowDate‘:nowDate,‘thDate‘:thDate,‘weekDate‘:weekDate,‘monthDate‘:monthDate}
}

js获取前三天,前七天,前一个月的时间

上一篇:01、微信公众号分享朋友圈或是朋友


下一篇:我的小程序终于完工