Last active
February 27, 2017 13:11
-
-
Save ltpitt/7e2e38d4142dcb5872ab0d77793f2e64 to your computer and use it in GitHub Desktop.
Javascript function that calculates the difference between two datetime in hours
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Calculates the difference between two datetime in hours | |
* @param {String} Current datetime (any format compatible with Date) | |
* @param {String} Start datetime (any format compatible with Date) | |
* @return {Integer} The difference between Current datetime and Start datetime in hours | |
*/ | |
function calculateDatetimeDeltaHours(datetimeCurrent, datetimeStart) { | |
var datetimeCurrent = new Date(datetimeCurrent); | |
var datetimeStart = new Date(datetimeStart); | |
var deltaHours = Math.abs(datetimeCurrent - datetimeStart) / 36e5 | |
return deltaHours; | |
} | |
// In the following example I put into deltaHours variable the return of calculateDatetimeDeltaHours function | |
var deltaHours = calculateDatetimeDeltaHours("04/13/2015 12:04:02", "04/13/2015 11:04:02"); | |
// Then I print the difference, in hours, between the two timestamps | |
console.log(deltaHours); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can try the code here:
https://jsfiddle.net/0opfanfL/