Skip to content

Instantly share code, notes, and snippets.

@dinizgb
Created June 30, 2023 14:35
Show Gist options
  • Save dinizgb/a9a58da28d7ed640f4377517398f2e59 to your computer and use it in GitHub Desktop.
Save dinizgb/a9a58da28d7ed640f4377517398f2e59 to your computer and use it in GitHub Desktop.
Javascript function to get the difference in Days between two dates.
/**
* 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