Js获取最近当日、昨天、近一周、近一月的时间

/* var list = this.getDateRange(6,true)console.log("获取近一周日期范围:\n开始日期:"+list[0]+";\n结束日期:"+list[1]);var list = this.getDateRange(30,true)console.log("获取近一个月日期范围:\n开始日期:"+list[0]+";\n结束日期:"+list[1]);var list = this.getDateRange(0,true)console.log("获取今天日期范围:\n开始日期:"+list[0]+";\n结束日期:"+list[1]);var list = this.getDateRange(1,true)console.log("获取昨天日期范围:\n开始日期:"+list[0]+";\n结束日期:"+list[0]);var list = this.getDateRange(6,false)console.log("获取下一周日期范围:\n开始日期:"+list[0]+";\n结束日期:"+list[1]);var list = this.getDateRange(30,false)console.log("获取下一个月日期范围:\n开始日期:"+list[0]+";\n结束日期:"+list[1]);*/
function  getDateRange(intervalDays,bolPastTime){var dateNow = new Date();let oneDayTime = 24 * 60 * 60 * 1000;let list = [];let lastDay;if(bolPastTime == true){lastDay = new Date(dateNow.getTime() - intervalDays * oneDayTime);list.push(formateDate(lastDay));list.push(formateDate(dateNow));}else{lastDay = new Date(dateNow.getTime() + intervalDays * oneDayTime);list.push(formateDate(dateNow));list.push(formateDate(lastDay));}return list;}//获取一段时间function formateDate(time){let year = time.getFullYear()let month = time.getMonth() + 1let day = time.getDate()if (month < 10) {month = '0' + month}if (day < 10) {day = '0' + day}return year + '/' + month + '/' + day + '  00/00/00'}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部