模拟下面图片显示的未来14天的日期:
image.png
let nowDate = new Date();
nowDate.setHours(0, 0, 0, 0)
// let timestamp1 = Date.parse(nowDate)
let timestamp = Date.parse(nowDate)
let arr = []
for (let i = 0; i < 14; i++){
let obj = {}
obj.time_stamp = timestamp
obj.time_str = `${new Date(obj.time_stamp).getFullYear()}-${new Date(obj.time_stamp).getMonth()+1 >= 10 ? new Date(obj.time_stamp).getMonth()+1 : '0' + new Date(obj.time_stamp).getMonth()+1}-${new Date(obj.time_stamp).getDate() >= 10 ? new Date(obj.time_stamp).getDate() : '0' + new Date(obj.time_stamp).getDate()}`
obj.time_date = `星期${'日一二三四五六'.charAt(new Date().getDay())}`
timestamp = timestamp + 86400 * 1000
arr.push(obj)
}
console.log(arr);
结果:
image.png
计算两个时间之间相差几天
function selectDay(from, to){
var begintime = new Date(Date.parse(from)).getTime();
var endtime = new Date(Date.parse(to)).getTime();
var nTime = endtime - begintime;
var day =Math.floor(nTime/86400000);
return day
}
selectDay('2018-10-22', '2018-11-04')
网友评论