es-next-tools
Version:
A comprehensive utility library for JavaScript and TypeScript that provides a wide range of functions for common programming tasks, including mathematical operations, date manipulations, array and object handling, string utilities, and more.
15 lines (14 loc) • 512 B
JavaScript
/**
* Calculates the difference in days between two dates.
* @param {Date} date1 - The first date.
* @param {Date} date2 - The second date.
* @returns {number} The number of days between the two dates.
* @example
* const date1 = new Date('2023-01-01');
* const date2 = new Date('2023-01-10');
* const difference = diff(date1, date2); // 9
*/
export function diff(date1, date2) {
const diffTime = Math.abs(date2.getTime() - date1.getTime());
return Math.ceil(diffTime / (1000 * 60 * 60 * 24));
}