Last active
June 16, 2022 13:30
-
-
Save brandonbarringer/b2c5f068f82cc951319a91f737c2fe9d to your computer and use it in GitHub Desktop.
Gets the difference between two times and returns an object with days, hours, minutes
This file contains hidden or 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
export const diffTIme = (time1, time2) => { | |
const date1 = new Date(time1); | |
const date2 = new Date(time2); | |
const diff = date2.getTime() - date1.getTime(); | |
let minutes = Math.floor(diff / (1000 * 60)) % 60; | |
let hours = 0; | |
let days = 0; | |
if (minutes > 59) { | |
hours = Math.floor(minutes / 60); | |
minutes %= 60; | |
} | |
if (hours > 23) { | |
days = Math.floor(hours / 24); | |
hours %= 24; | |
} | |
return { | |
days, | |
hours, | |
minutes, | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment