Skip to content

Instantly share code, notes, and snippets.

@malikalichsan
Last active June 18, 2020 06:38
Show Gist options
  • Save malikalichsan/2f52b8c52665df6297d8f26e5ee218ef to your computer and use it in GitHub Desktop.
Save malikalichsan/2f52b8c52665df6297d8f26e5ee218ef to your computer and use it in GitHub Desktop.
Calculation between times in Javascript using moment.js
const calculationBetweenTimes = (startTime, endTime) => {
let diff = moment(endTime, 'HH:mm').diff(moment(startTime, 'HH:mm'))
let d = moment.duration(diff);
let hours = Math.floor(d.asHours());
let minutes = moment.utc(diff).format("mm");
return {
hours: hours,
minutes: minutes,
}
};
let calculation = calculationBetweenTimes("15:00", "17:30");
console.log(calculation);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment