Skip to content

Instantly share code, notes, and snippets.

@mreis1
Created August 7, 2020 18:20
Show Gist options
  • Select an option

  • Save mreis1/8ae987002f1ea280c9c634bf17fbdae5 to your computer and use it in GitHub Desktop.

Select an option

Save mreis1/8ae987002f1ea280c9c634bf17fbdae5 to your computer and use it in GitHub Desktop.
Calculate number of days and time between two dates
/**
diffDates returning number of days and time string
*/
function diffDates(a,b) {
var d = moment(a).diff(b);
var m = d / (3600*1000*24);
var m1 = Math.floor(m);
var t = d-(m1*3600*1000*24);
return {
days: m1,
time: t,
timeStr: moment(t).utc().format('HH:mm')
}
}
diffDates(
moment(), moment().add(-1,'day').add(-10,'minutes'))
// Output = 1day e 10minutos
{days: 1, time: 600000, timeStr: "00:10"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment