首页 > Web开发 > 详细

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

时间:2021-05-24 09:41:20      阅读:30      评论:0      收藏:0      [点我收藏+]

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获取前三天,前七天,前一个月的时间

原文:https://www.cnblogs.com/alannero/p/14802684.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!