Skip to content

Instantly share code, notes, and snippets.

@brandonbarringer
Last active June 16, 2022 13:30
Show Gist options
  • Save brandonbarringer/b2c5f068f82cc951319a91f737c2fe9d to your computer and use it in GitHub Desktop.
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
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