Created
October 31, 2018 12:07
-
-
Save jastisriradheshyam/10fdec4ecb9a0cb775fcdbb0d33fd40b to your computer and use it in GitHub Desktop.
date difference js
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
// Reference - https://www.w3resource.com/javascript-exercises/javascript-date-exercise-8.php | |
var date_diff_inseconds = function (date1, date2) { | |
let dt1 = new Date(date1); | |
let dt2 = new Date(date2); | |
return Math.floor((Date.UTC(dt2.getFullYear(), dt2.getMonth(), dt2.getDate(), dt2.getHours(), dt2.getMinutes(), dt2.getSeconds()) - Date.UTC(dt1.getFullYear(), dt1.getMonth(), dt1.getDate(), dt1.getHours(), dt1.getMinutes(), dt1.getSeconds())) / (1000)); | |
}; | |
var date_diff_inminutes = function (date1, date2) { | |
let dt1 = new Date(date1); | |
let dt2 = new Date(date2); | |
return Math.floor((Date.UTC(dt2.getFullYear(), dt2.getMonth(), dt2.getDate(), dt2.getHours(), dt2.getMinutes()) - Date.UTC(dt1.getFullYear(), dt1.getMonth(), dt1.getDate(), dt1.getHours(), dt1.getMinutes())) / (1000 * 60)); | |
}; | |
var date_diff_inhours = function (date1, date2) { | |
let dth1 = new Date(date1); | |
let dth2 = new Date(date2); | |
return Math.floor((Date.UTC(dth2.getFullYear(), dth2.getMonth(), dth2.getDate(), dth2.getHours()) - Date.UTC(dth1.getFullYear(), dth1.getMonth(), dth1.getDate(), dth1.getHours())) / (1000 * 60 * 60)); | |
}; | |
var date_diff_indays = function (date1, date2) { | |
let dt1 = new Date(date1); | |
let dt2 = new Date(date2); | |
return Math.floor((Date.UTC(dt2.getFullYear(), dt2.getMonth(), dt2.getDate()) - Date.UTC(dt1.getFullYear(), dt1.getMonth(), dt1.getDate())) / (1000 * 60 * 60 * 24)); | |
}; |
Author
jastisriradheshyam
commented
Feb 25, 2019
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment