Created
June 30, 2023 14:35
-
-
Save dinizgb/a9a58da28d7ed640f4377517398f2e59 to your computer and use it in GitHub Desktop.
Javascript function to get the difference in Days between two dates.
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
/** | |
* Function to get the difference in Days between two dates | |
* @param {string} date1 - With the date 1 (With at least yyyy-mm-dd date format) | |
* @param {number} date2 - With the date 2 (With at least yyyy-mm-dd date format) | |
* @returns {number} With the difference in days | |
*/ | |
export const differenceInDays = (date1: string, date2: string): number => { | |
const d1 = new Date(date1); | |
const d2 = new Date(date2); | |
const timeDiff = d2.getTime() - d1.getTime(); | |
const dayDiff = timeDiff / (1000 * 3600 * 24); | |
return Math.ceil(dayDiff); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment