Skip to content

Instantly share code, notes, and snippets.

@MBlore
Last active September 11, 2022 01:40
Show Gist options
  • Save MBlore/08d4556ac81b4a254d80aac344065e32 to your computer and use it in GitHub Desktop.
Save MBlore/08d4556ac81b4a254d80aac344065e32 to your computer and use it in GitHub Desktop.
JS Time difference between two dates.
/**
* Returns the amount of seconds between two given dates.
* @param {Date} dtStart The start time.
* @param {Date} dtEnd The end time.
* @returns The number of seconds between the two dates.
*/
function secondsBetween(dtStart, dtEnd) {
var diff = (dtStart.getTime() - dtEnd.getTime()) / 1000;
return Math.abs(diff);
}
/**
* Returns the amount of minutes between two given dates.
* @param {Date} dtStart The start time.
* @param {Date} dtEnd The end time.
* @returns The number of minutes between the two dates.
*/
function minutesBetween(dtStart, dtEnd) {
var diff = (dtStart.getTime() - dtEnd.getTime()) / 1000;
diff /= 60;
return Math.abs(diff);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment