Skip to content

Instantly share code, notes, and snippets.

@johnmurch
Last active November 5, 2020 17:56
Show Gist options
  • Save johnmurch/d649f311fb489e1f6945018329354f17 to your computer and use it in GitHub Desktop.
Save johnmurch/d649f311fb489e1f6945018329354f17 to your computer and use it in GitHub Desktop.
dd/mm/yyyy hh:mm:ss
var d = new Date();
alert(
("00" + (d.getMonth() + 1)).slice(-2) + "/" +
("00" + d.getDate()).slice(-2) + "/" +
d.getFullYear() + " " +
("00" + d.getHours()).slice(-2) + ":" +
("00" + d.getMinutes()).slice(-2) + ":" +
("00" + d.getSeconds()).slice(-2)
);
// https://stackoverflow.com/questions/13903897/javascript-return-number-of-days-hours-minutes-seconds-between-two-dates
console.log(duration('2019-07-17T18:35:25.235Z', '2019-07-20T00:37:28.839Z'));
function duration(t0, t1){
let d = (new Date(t1)) - (new Date(t0));
let weekdays = Math.floor(d/1000/60/60/24/7);
let days = Math.floor(d/1000/60/60/24 - weekdays*7);
let hours = Math.floor(d/1000/60/60 - weekdays*7*24 - days*24);
let minutes = Math.floor(d/1000/60 - weekdays*7*24*60 - days*24*60 - hours*60);
let seconds = Math.floor(d/1000 - weekdays*7*24*60*60 - days*24*60*60 - hours*60*60 - minutes*60);
let milliseconds = Math.floor(d - weekdays*7*24*60*60*1000 - days*24*60*60*1000 - hours*60*60*1000 - minutes*60*1000 - seconds*1000);
let t = {};
['weekdays', 'days', 'hours', 'minutes', 'seconds', 'milliseconds'].forEach(q=>{ if (eval(q)>0) { t[q] = eval(q); } });
return t;
}
/*
{
"days": 2,
"hours": 6,
"minutes": 2,
"seconds": 3,
"milliseconds": 604
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment