我想得到日期差异:
var dateString='2015-04-07T10:46:25Z';
var dt = new Date(value);
var now = new Date();
var _MS_PER_DAY = 1000 * 60 * 60 * 24;
var utc1 = Date.UTC(dt.getFullYear(), dt.getMonth(), dt.getDate());
var utc2 = Date.UTC(now.getFullYear(), now.getMonth(), now.getDate());
var days=Math.floor((utc2 - utc1) / _MS_PER_DAY);//this is 415
结果必须采用以下格式:415d,03:06:33
最好的方法是什么?我得到约会,但时间不见了。
答案 0 :(得分:0)
Try this
var datetime = new Date("2015-04-07T10:46:25Z");
var now = new Date();
if (datetime < now) {
var diffTime = now - datetime;
}else{
var diffTime = datetime - now;
}
var days = Math.floor(diffTime / 1000 / 60 / (60 * 24));
var dateDiff = new Date( diffTime );
var hour = dateDiff .getHours().toString().length == 1 ? '0' + dateDiff .getHours() : dateDiff .getHours();
var minute = dateDiff.getMinutes().toString().length == 1 ? '0' + dateDiff.getMinutes() : dateDiff.getMinutes();
var seconds = dateDiff.getSeconds().toString().length == 1 ? '0' + dateDiff.getSeconds() : dateDiff.getSeconds();
console.log(days + "d "+ hour + ":" + minute + ":" + seconds);
答案 1 :(得分:0)
您需要使用模数
var dt = new Date('2015-04-07T10:46:25Z');
var now = new Date();
var milSecondMil = 1000;
var secondMil = milSecondMil * 60;
var hourMil = secondMil * 60;
var dayMil = hourMil * 24;
var diff = now - dt;
var days = Math.floor(diff / dayMil);
var daysRemainder = diff % dayMil;
var hours = Math.floor(daysRemainder / hourMil);
var hoursRemainder = daysRemainder % hourMil;
var seconds = Math.floor(hoursRemainder / secondMil);
var secondsRemainder = hoursRemainder % secondMil;
var milSeconds = Math.floor(secondsRemainder / milSecondMil);
console.log(days + ' days - ' + hours + ' hours - ' + seconds + ' seconds - ' + milSeconds + ' mil');
答案 2 :(得分:0)
我这样做了:
var dt = new Date(value);
var now = new Date();
var date1_ms = dt.getTime();
var date2_ms = now.getTime();
var diff=(date2_ms - date1_ms) / 1000;
var tm = new Date(null, null, null, null, null, Math.floor(diff % 86400)).toTimeString().split(" ")[0];
return Math.round(diff / (60 * 60 * 24)) + 'd, ' + tm;