JS计算某个日期距今时间

JS计算某个日期距今时间

1、做个日常笔记,

//兼容IE8
// time格式为 '2018-08-01或2018/08/01或2018/8/1'
var daysComputed = function(time) {
    var oldTimeFormat = new Date(time.replace(/-/g, '/'));
    var nowDate = new Date();
    if(nowDate.getTime() - oldTimeFormat.getTime() > 0) {
        var times = nowDate.getTime() - oldTimeFormat.getTime();
        var days = parseInt(times / (60*60*24*1000));
        return days;
    } else {
        throw('传入时间不能大于当前日期');
    }
}
daysComputed('2017-11-1');



// time格式为 '2018-08-01或2018/08/01或2018/8/1'
const daysComputed = time => {
    let oldTimeFormat = new Date(time)
    let nowDate = new Date()
    if(nowDate.getTime() - oldTimeFormat.getTime() > 0) {
        let times = nowDate.getTime() - oldTimeFormat.getTime()
        let days = parseInt(times / (60*60*24*1000))
        return days
    } else {
        throw('传入时间不能大于当前日期')
    }
}
daysComputed('2017-11-1')
上一篇:js 动态校验开始结束时间


下一篇:都在写抖音上的网红罗盘时钟,我也用vue仿写一个