// use a constant date (e.g. 2000-01-01) and the desired time to initialize two dates
var date1 = new Date(2000, 0, 1, 9, 0); // 9:00 AM
var date2 = new Date(2000, 0, 1, 17, 0); // 5:00 PM
// the following is to handle cases where the times are on the opposite side of
// midnight e.g. when you want to get the difference between 9:00 PM and 5:00 AM
if (date2 < date1) {
date2.setDate(date2.getDate() + 1);
}
var diff = date2 - date1;
// 28800000 milliseconds (8 hours)
然后你可以像下面这样把毫秒转换成小时、分钟和秒:
var msec = diff;
var hh = Math.floor(msec / 1000 / 60 / 60);
msec -= hh * 1000 * 60 * 60;
var mm = Math.floor(msec / 1000 / 60);
msec -= mm * 1000 * 60;
var ss = Math.floor(msec / 1000);
msec -= ss * 1000;
// diff = 28800000 => hh = 8, mm = 0, ss = 0, msec = 0
你可以像下面这样把时间转换成24小时格式的字符串:
function parseTime(s) {
var part = s.match(/(\d+):(\d+)(?: )?(am|pm)?/i);
var hh = parseInt(part[1], 10);
var mm = parseInt(part[2], 10);
var ap = part[3] ? part[3].toUpperCase() : null;
if (ap === "AM") {
if (hh == 12) {
hh = 0;
}
}
if (ap === "PM") {
if (hh != 12) {
hh += 12;
}
}
return { hh: hh, mm: mm };
}
parseTime("12:00 AM"); // {hh: 0, mm: 0}
parseTime("12:00 PM"); // {hh: 12, mm: 0}
parseTime("01:00 PM"); // {hh: 13, mm: 0}
parseTime("23:00"); // {hh: 23, mm: 0}
var now = new Date();
var future = new Date(now.setMinutes(15));
var futureEpoch = moment(future).unix();
var nowEpoch = moment(now).unix();
var differenceInEpoch = nowEpoch - scheduledEpoch ;
console.log("futureEpoch : " + futureEpoch);
console.log("nowEpoch : " + nowEpoch);
console.log("differenceInEpoch : " + differenceInEpoch);
var diffTime = new Date(0); // The 0 there is the key, which sets the date to the epoch
diffTime.setUTCSeconds(differenceInEpoch);
console.log("new diffTime : " + diffTime);
function timeDiffInHours(milliseconds){
time_diff = (new Date).getTime() - milliseconds
return parseInt((time_diff/(1000*60*60)) % 24)
}
// This is again sending current time and diff would be 0.
timeDiffInHours((new Date).getTime());
//specified date:
var oneDate = new Date("November 02, 2017 06:00:00");
//number of milliseconds since midnight Jan 1 1970 till specified date
var oneDateMiliseconds = oneDate.getTime();
////number of milliseconds since midnight Jan 1 1970 till now
var currentMiliseconds = Date.now();
//return time difference in miliseconds
alert(currentMiliseconds-oneDateMiliseconds);
/**
* Get Two Time Different
* @param join
* @param lastSeen
* @param now
* @returns {string}
*/
function getTimeDiff( join, lastSeen, now = false)
{
let t1 = new Date(join).getTime(), t2 = new Date(lastSeen).getTime(), milliseconds =0, time ='';
if (now) t2 = Date.now();
if( isNaN(t1) || isNaN(t2) ) return '';
if (t1 < t2) milliseconds = t2 - t1; else milliseconds = t1 - t2;
var days = Math.floor(milliseconds / 1000 / 60 / (60 * 24));
var date_diff = new Date( milliseconds );
if (days > 0) time += days + 'd ';
if (date_diff.getHours() > 0) time += date_diff.getHours() + 'h ';
if (date_diff.getMinutes() > 0) time += date_diff.getMinutes() + 'm ';
if (date_diff.getSeconds() > 0) time += date_diff.getSeconds() + 's ';
return time;
}
console.log(getTimeDiff(1578852606608, 1579530945513));
var moment = require("moment");
var momentDurationFormatSetup = require("moment-duration-format")
var now = "2015-07-16T16:33:39.113Z";
var then = "2015-06-16T22:33:39.113Z";
var ms = moment(now,"YYYY-MM-DD'T'HH:mm:ss:SSSZ").diff(moment(then,"YYYY-MM-DD'T'HH:mm:ss:SSSZ"));
var d = moment.duration(ms);
var s = d.format("dd:hh:mm:ss");
console.log(s);