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')
标签:oldTimeFormat,getTime,JS,var,nowDate,2018,time,某个,距今 来源: https://blog.csdn.net/ZhouLoverBrother/article/details/114833526