美文网首页
模拟从当天起未来两周的日期数据

模拟从当天起未来两周的日期数据

作者: 5df463a52098 | 来源:发表于2018-10-22 13:38 被阅读24次

    模拟下面图片显示的未来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')
    

    相关文章

      网友评论

          本文标题:模拟从当天起未来两周的日期数据

          本文链接:https://www.haomeiwen.com/subject/brqgzftx.html