UNPKG

chartjs-adapter-date-fns

Version:

Chart.js adapter to use date-fns for time functionalities

1,458 lines (1,348 loc) 221 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(require('chart.js')) : typeof define === 'function' && define.amd ? define(['chart.js'], factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.Chart)); }(this, (function (chart_js) { 'use strict'; function toInteger(dirtyNumber) { if (dirtyNumber === null || dirtyNumber === true || dirtyNumber === false) { return NaN; } var number = Number(dirtyNumber); if (isNaN(number)) { return number; } return number < 0 ? Math.ceil(number) : Math.floor(number); } function requiredArgs(required, args) { if (args.length < required) { throw new TypeError(required + ' argument' + (required > 1 ? 's' : '') + ' required, but only ' + args.length + ' present'); } } /** * @name toDate * @category Common Helpers * @summary Convert the given argument to an instance of Date. * * @description * Convert the given argument to an instance of Date. * * If the argument is an instance of Date, the function returns its clone. * * If the argument is a number, it is treated as a timestamp. * * If the argument is none of the above, the function returns Invalid Date. * * **Note**: *all* Date arguments passed to any *date-fns* function is processed by `toDate`. * * @param {Date|Number} argument - the value to convert * @returns {Date} the parsed date in the local time zone * @throws {TypeError} 1 argument required * * @example * // Clone the date: * const result = toDate(new Date(2014, 1, 11, 11, 30, 30)) * //=> Tue Feb 11 2014 11:30:30 * * @example * // Convert the timestamp to date: * const result = toDate(1392098430000) * //=> Tue Feb 11 2014 11:30:30 */ function toDate(argument) { requiredArgs(1, arguments); var argStr = Object.prototype.toString.call(argument); // Clone the date if (argument instanceof Date || typeof argument === 'object' && argStr === '[object Date]') { // Prevent the date to lose the milliseconds when passed to new Date() in IE10 return new Date(argument.getTime()); } else if (typeof argument === 'number' || argStr === '[object Number]') { return new Date(argument); } else { if ((typeof argument === 'string' || argStr === '[object String]') && typeof console !== 'undefined') { // eslint-disable-next-line no-console console.warn("Starting with v2.0.0-beta.1 date-fns doesn't accept strings as date arguments. Please use `parseISO` to parse strings. See: https://git.io/fjule"); // eslint-disable-next-line no-console console.warn(new Error().stack); } return new Date(NaN); } } /** * @name addDays * @category Day Helpers * @summary Add the specified number of days to the given date. * * @description * Add the specified number of days to the given date. * * ### v2.0.0 breaking changes: * * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes). * * @param {Date|Number} date - the date to be changed * @param {Number} amount - the amount of days to be added. Positive decimals will be rounded using `Math.floor`, decimals less than zero will be rounded using `Math.ceil`. * @returns {Date} the new date with the days added * @throws {TypeError} 2 arguments required * * @example * // Add 10 days to 1 September 2014: * const result = addDays(new Date(2014, 8, 1), 10) * //=> Thu Sep 11 2014 00:00:00 */ function addDays(dirtyDate, dirtyAmount) { requiredArgs(2, arguments); var date = toDate(dirtyDate); var amount = toInteger(dirtyAmount); if (isNaN(amount)) { return new Date(NaN); } if (!amount) { // If 0 days, no-op to avoid changing times in the hour before end of DST return date; } date.setDate(date.getDate() + amount); return date; } /** * @name addMonths * @category Month Helpers * @summary Add the specified number of months to the given date. * * @description * Add the specified number of months to the given date. * * ### v2.0.0 breaking changes: * * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes). * * @param {Date|Number} date - the date to be changed * @param {Number} amount - the amount of months to be added. Positive decimals will be rounded using `Math.floor`, decimals less than zero will be rounded using `Math.ceil`. * @returns {Date} the new date with the months added * @throws {TypeError} 2 arguments required * * @example * // Add 5 months to 1 September 2014: * const result = addMonths(new Date(2014, 8, 1), 5) * //=> Sun Feb 01 2015 00:00:00 */ function addMonths(dirtyDate, dirtyAmount) { requiredArgs(2, arguments); var date = toDate(dirtyDate); var amount = toInteger(dirtyAmount); if (isNaN(amount)) { return new Date(NaN); } if (!amount) { // If 0 months, no-op to avoid changing times in the hour before end of DST return date; } var dayOfMonth = date.getDate(); // The JS Date object supports date math by accepting out-of-bounds values for // month, day, etc. For example, new Date(2020, 1, 0) returns 31 Dec 2019 and // new Date(2020, 13, 1) returns 1 Feb 2021. This is *almost* the behavior we // want except that dates will wrap around the end of a month, meaning that // new Date(2020, 13, 31) will return 3 Mar 2021 not 28 Feb 2021 as desired. So // we'll default to the end of the desired month by adding 1 to the desired // month and using a date of 0 to back up one day to the end of the desired // month. var endOfDesiredMonth = new Date(date.getTime()); endOfDesiredMonth.setMonth(date.getMonth() + amount + 1, 0); var daysInMonth = endOfDesiredMonth.getDate(); if (dayOfMonth >= daysInMonth) { // If we're already at the end of the month, then this is the correct date // and we're done. return endOfDesiredMonth; } else { // Otherwise, we now know that setting the original day-of-month value won't // cause an overflow, so set the desired day-of-month. Note that we can't // just set the date of `endOfDesiredMonth` because that object may have had // its time changed in the unusual case where where a DST transition was on // the last day of the month and its local time was in the hour skipped or // repeated next to a DST transition. So we use `date` instead which is // guaranteed to still have the original time. date.setFullYear(endOfDesiredMonth.getFullYear(), endOfDesiredMonth.getMonth(), dayOfMonth); return date; } } /** * @name addMilliseconds * @category Millisecond Helpers * @summary Add the specified number of milliseconds to the given date. * * @description * Add the specified number of milliseconds to the given date. * * ### v2.0.0 breaking changes: * * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes). * * @param {Date|Number} date - the date to be changed * @param {Number} amount - the amount of milliseconds to be added. Positive decimals will be rounded using `Math.floor`, decimals less than zero will be rounded using `Math.ceil`. * @returns {Date} the new date with the milliseconds added * @throws {TypeError} 2 arguments required * * @example * // Add 750 milliseconds to 10 July 2014 12:45:30.000: * const result = addMilliseconds(new Date(2014, 6, 10, 12, 45, 30, 0), 750) * //=> Thu Jul 10 2014 12:45:30.750 */ function addMilliseconds(dirtyDate, dirtyAmount) { requiredArgs(2, arguments); var timestamp = toDate(dirtyDate).getTime(); var amount = toInteger(dirtyAmount); return new Date(timestamp + amount); } var MILLISECONDS_IN_HOUR$3 = 3600000; /** * @name addHours * @category Hour Helpers * @summary Add the specified number of hours to the given date. * * @description * Add the specified number of hours to the given date. * * ### v2.0.0 breaking changes: * * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes). * * @param {Date|Number} date - the date to be changed * @param {Number} amount - the amount of hours to be added. Positive decimals will be rounded using `Math.floor`, decimals less than zero will be rounded using `Math.ceil`. * @returns {Date} the new date with the hours added * @throws {TypeError} 2 arguments required * * @example * // Add 2 hours to 10 July 2014 23:00:00: * const result = addHours(new Date(2014, 6, 10, 23, 0), 2) * //=> Fri Jul 11 2014 01:00:00 */ function addHours(dirtyDate, dirtyAmount) { requiredArgs(2, arguments); var amount = toInteger(dirtyAmount); return addMilliseconds(dirtyDate, amount * MILLISECONDS_IN_HOUR$3); } /** * @name startOfWeek * @category Week Helpers * @summary Return the start of a week for the given date. * * @description * Return the start of a week for the given date. * The result will be in the local timezone. * * ### v2.0.0 breaking changes: * * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes). * * @param {Date|Number} date - the original date * @param {Object} [options] - an object with options. * @param {Locale} [options.locale=defaultLocale] - the locale object. See [Locale]{@link https://date-fns.org/docs/Locale} * @param {0|1|2|3|4|5|6} [options.weekStartsOn=0] - the index of the first day of the week (0 - Sunday) * @returns {Date} the start of a week * @throws {TypeError} 1 argument required * @throws {RangeError} `options.weekStartsOn` must be between 0 and 6 * * @example * // The start of a week for 2 September 2014 11:55:00: * var result = startOfWeek(new Date(2014, 8, 2, 11, 55, 0)) * //=> Sun Aug 31 2014 00:00:00 * * @example * // If the week starts on Monday, the start of the week for 2 September 2014 11:55:00: * var result = startOfWeek(new Date(2014, 8, 2, 11, 55, 0), { weekStartsOn: 1 }) * //=> Mon Sep 01 2014 00:00:00 */ function startOfWeek(dirtyDate, dirtyOptions) { requiredArgs(1, arguments); var options = dirtyOptions || {}; var locale = options.locale; var localeWeekStartsOn = locale && locale.options && locale.options.weekStartsOn; var defaultWeekStartsOn = localeWeekStartsOn == null ? 0 : toInteger(localeWeekStartsOn); var weekStartsOn = options.weekStartsOn == null ? defaultWeekStartsOn : toInteger(options.weekStartsOn); // Test if weekStartsOn is between 0 and 6 _and_ is not NaN if (!(weekStartsOn >= 0 && weekStartsOn <= 6)) { throw new RangeError('weekStartsOn must be between 0 and 6 inclusively'); } var date = toDate(dirtyDate); var day = date.getDay(); var diff = (day < weekStartsOn ? 7 : 0) + day - weekStartsOn; date.setDate(date.getDate() - diff); date.setHours(0, 0, 0, 0); return date; } /** * Google Chrome as of 67.0.3396.87 introduced timezones with offset that includes seconds. * They usually appear for dates that denote time before the timezones were introduced * (e.g. for 'Europe/Prague' timezone the offset is GMT+00:57:44 before 1 October 1891 * and GMT+01:00:00 after that date) * * Date#getTimezoneOffset returns the offset in minutes and would return 57 for the example above, * which would lead to incorrect calculations. * * This function returns the timezone offset in milliseconds that takes seconds in account. */ function getTimezoneOffsetInMilliseconds(date) { var utcDate = new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds(), date.getMilliseconds())); utcDate.setUTCFullYear(date.getFullYear()); return date.getTime() - utcDate.getTime(); } /** * @name startOfDay * @category Day Helpers * @summary Return the start of a day for the given date. * * @description * Return the start of a day for the given date. * The result will be in the local timezone. * * ### v2.0.0 breaking changes: * * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes). * * @param {Date|Number} date - the original date * @returns {Date} the start of a day * @throws {TypeError} 1 argument required * * @example * // The start of a day for 2 September 2014 11:55:00: * const result = startOfDay(new Date(2014, 8, 2, 11, 55, 0)) * //=> Tue Sep 02 2014 00:00:00 */ function startOfDay(dirtyDate) { requiredArgs(1, arguments); var date = toDate(dirtyDate); date.setHours(0, 0, 0, 0); return date; } var MILLISECONDS_IN_DAY$1 = 86400000; /** * @name differenceInCalendarDays * @category Day Helpers * @summary Get the number of calendar days between the given dates. * * @description * Get the number of calendar days between the given dates. This means that the times are removed * from the dates and then the difference in days is calculated. * * ### v2.0.0 breaking changes: * * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes). * * @param {Date|Number} dateLeft - the later date * @param {Date|Number} dateRight - the earlier date * @returns {Number} the number of calendar days * @throws {TypeError} 2 arguments required * * @example * // How many calendar days are between * // 2 July 2011 23:00:00 and 2 July 2012 00:00:00? * var result = differenceInCalendarDays( * new Date(2012, 6, 2, 0, 0), * new Date(2011, 6, 2, 23, 0) * ) * //=> 366 * // How many calendar days are between * // 2 July 2011 23:59:00 and 3 July 2011 00:01:00? * var result = differenceInCalendarDays( * new Date(2011, 6, 3, 0, 1), * new Date(2011, 6, 2, 23, 59) * ) * //=> 1 */ function differenceInCalendarDays(dirtyDateLeft, dirtyDateRight) { requiredArgs(2, arguments); var startOfDayLeft = startOfDay(dirtyDateLeft); var startOfDayRight = startOfDay(dirtyDateRight); var timestampLeft = startOfDayLeft.getTime() - getTimezoneOffsetInMilliseconds(startOfDayLeft); var timestampRight = startOfDayRight.getTime() - getTimezoneOffsetInMilliseconds(startOfDayRight); // Round the number of days to the nearest integer // because the number of milliseconds in a day is not constant // (e.g. it's different in the day of the daylight saving time clock shift) return Math.round((timestampLeft - timestampRight) / MILLISECONDS_IN_DAY$1); } var MILLISECONDS_IN_MINUTE$3 = 60000; /** * @name addMinutes * @category Minute Helpers * @summary Add the specified number of minutes to the given date. * * @description * Add the specified number of minutes to the given date. * * ### v2.0.0 breaking changes: * * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes). * * @param {Date|Number} date - the date to be changed * @param {Number} amount - the amount of minutes to be added. Positive decimals will be rounded using `Math.floor`, decimals less than zero will be rounded using `Math.ceil`. * @returns {Date} the new date with the minutes added * @throws {TypeError} 2 arguments required * * @example * // Add 30 minutes to 10 July 2014 12:00:00: * const result = addMinutes(new Date(2014, 6, 10, 12, 0), 30) * //=> Thu Jul 10 2014 12:30:00 */ function addMinutes(dirtyDate, dirtyAmount) { requiredArgs(2, arguments); var amount = toInteger(dirtyAmount); return addMilliseconds(dirtyDate, amount * MILLISECONDS_IN_MINUTE$3); } /** * @name addQuarters * @category Quarter Helpers * @summary Add the specified number of year quarters to the given date. * * @description * Add the specified number of year quarters to the given date. * * ### v2.0.0 breaking changes: * * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes). * * @param {Date|Number} date - the date to be changed * @param {Number} amount - the amount of quarters to be added. Positive decimals will be rounded using `Math.floor`, decimals less than zero will be rounded using `Math.ceil`. * @returns {Date} the new date with the quarters added * @throws {TypeError} 2 arguments required * * @example * // Add 1 quarter to 1 September 2014: * const result = addQuarters(new Date(2014, 8, 1), 1) * //=> Mon Dec 01 2014 00:00:00 */ function addQuarters(dirtyDate, dirtyAmount) { requiredArgs(2, arguments); var amount = toInteger(dirtyAmount); var months = amount * 3; return addMonths(dirtyDate, months); } /** * @name addSeconds * @category Second Helpers * @summary Add the specified number of seconds to the given date. * * @description * Add the specified number of seconds to the given date. * * ### v2.0.0 breaking changes: * * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes). * * @param {Date|Number} date - the date to be changed * @param {Number} amount - the amount of seconds to be added. Positive decimals will be rounded using `Math.floor`, decimals less than zero will be rounded using `Math.ceil`. * @returns {Date} the new date with the seconds added * @throws {TypeError} 2 arguments required * * @example * // Add 30 seconds to 10 July 2014 12:45:00: * const result = addSeconds(new Date(2014, 6, 10, 12, 45, 0), 30) * //=> Thu Jul 10 2014 12:45:30 */ function addSeconds(dirtyDate, dirtyAmount) { requiredArgs(2, arguments); var amount = toInteger(dirtyAmount); return addMilliseconds(dirtyDate, amount * 1000); } /** * @name addWeeks * @category Week Helpers * @summary Add the specified number of weeks to the given date. * * @description * Add the specified number of week to the given date. * * ### v2.0.0 breaking changes: * * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes). * * @param {Date|Number} date - the date to be changed * @param {Number} amount - the amount of weeks to be added. Positive decimals will be rounded using `Math.floor`, decimals less than zero will be rounded using `Math.ceil`. * @returns {Date} the new date with the weeks added * @throws {TypeError} 2 arguments required * * @example * // Add 4 weeks to 1 September 2014: * const result = addWeeks(new Date(2014, 8, 1), 4) * //=> Mon Sep 29 2014 00:00:00 */ function addWeeks(dirtyDate, dirtyAmount) { requiredArgs(2, arguments); var amount = toInteger(dirtyAmount); var days = amount * 7; return addDays(dirtyDate, days); } /** * @name addYears * @category Year Helpers * @summary Add the specified number of years to the given date. * * @description * Add the specified number of years to the given date. * * ### v2.0.0 breaking changes: * * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes). * * @param {Date|Number} date - the date to be changed * @param {Number} amount - the amount of years to be added. Positive decimals will be rounded using `Math.floor`, decimals less than zero will be rounded using `Math.ceil`. * @returns {Date} the new date with the years added * @throws {TypeError} 2 arguments required * * @example * // Add 5 years to 1 September 2014: * const result = addYears(new Date(2014, 8, 1), 5) * //=> Sun Sep 01 2019 00:00:00 */ function addYears(dirtyDate, dirtyAmount) { requiredArgs(2, arguments); var amount = toInteger(dirtyAmount); return addMonths(dirtyDate, amount * 12); } /** * @name compareAsc * @category Common Helpers * @summary Compare the two dates and return -1, 0 or 1. * * @description * Compare the two dates and return 1 if the first date is after the second, * -1 if the first date is before the second or 0 if dates are equal. * * ### v2.0.0 breaking changes: * * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes). * * @param {Date|Number} dateLeft - the first date to compare * @param {Date|Number} dateRight - the second date to compare * @returns {Number} the result of the comparison * @throws {TypeError} 2 arguments required * * @example * // Compare 11 February 1987 and 10 July 1989: * const result = compareAsc(new Date(1987, 1, 11), new Date(1989, 6, 10)) * //=> -1 * * @example * // Sort the array of dates: * const result = [ * new Date(1995, 6, 2), * new Date(1987, 1, 11), * new Date(1989, 6, 10) * ].sort(compareAsc) * //=> [ * // Wed Feb 11 1987 00:00:00, * // Mon Jul 10 1989 00:00:00, * // Sun Jul 02 1995 00:00:00 * // ] */ function compareAsc(dirtyDateLeft, dirtyDateRight) { requiredArgs(2, arguments); var dateLeft = toDate(dirtyDateLeft); var dateRight = toDate(dirtyDateRight); var diff = dateLeft.getTime() - dateRight.getTime(); if (diff < 0) { return -1; } else if (diff > 0) { return 1; // Return 0 if diff is 0; return NaN if diff is NaN } else { return diff; } } /** * @name isValid * @category Common Helpers * @summary Is the given date valid? * * @description * Returns false if argument is Invalid Date and true otherwise. * Argument is converted to Date using `toDate`. See [toDate]{@link https://date-fns.org/docs/toDate} * Invalid Date is a Date, whose time value is NaN. * * Time value of Date: http://es5.github.io/#x15.9.1.1 * * ### v2.0.0 breaking changes: * * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes). * * - Now `isValid` doesn't throw an exception * if the first argument is not an instance of Date. * Instead, argument is converted beforehand using `toDate`. * * Examples: * * | `isValid` argument | Before v2.0.0 | v2.0.0 onward | * |---------------------------|---------------|---------------| * | `new Date()` | `true` | `true` | * | `new Date('2016-01-01')` | `true` | `true` | * | `new Date('')` | `false` | `false` | * | `new Date(1488370835081)` | `true` | `true` | * | `new Date(NaN)` | `false` | `false` | * | `'2016-01-01'` | `TypeError` | `false` | * | `''` | `TypeError` | `false` | * | `1488370835081` | `TypeError` | `true` | * | `NaN` | `TypeError` | `false` | * * We introduce this change to make *date-fns* consistent with ECMAScript behavior * that try to coerce arguments to the expected type * (which is also the case with other *date-fns* functions). * * @param {*} date - the date to check * @returns {Boolean} the date is valid * @throws {TypeError} 1 argument required * * @example * // For the valid date: * var result = isValid(new Date(2014, 1, 31)) * //=> true * * @example * // For the value, convertable into a date: * var result = isValid(1393804800000) * //=> true * * @example * // For the invalid date: * var result = isValid(new Date('')) * //=> false */ function isValid(dirtyDate) { requiredArgs(1, arguments); var date = toDate(dirtyDate); return !isNaN(date); } /** * @name differenceInCalendarMonths * @category Month Helpers * @summary Get the number of calendar months between the given dates. * * @description * Get the number of calendar months between the given dates. * * ### v2.0.0 breaking changes: * * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes). * * @param {Date|Number} dateLeft - the later date * @param {Date|Number} dateRight - the earlier date * @returns {Number} the number of calendar months * @throws {TypeError} 2 arguments required * * @example * // How many calendar months are between 31 January 2014 and 1 September 2014? * var result = differenceInCalendarMonths( * new Date(2014, 8, 1), * new Date(2014, 0, 31) * ) * //=> 8 */ function differenceInCalendarMonths(dirtyDateLeft, dirtyDateRight) { requiredArgs(2, arguments); var dateLeft = toDate(dirtyDateLeft); var dateRight = toDate(dirtyDateRight); var yearDiff = dateLeft.getFullYear() - dateRight.getFullYear(); var monthDiff = dateLeft.getMonth() - dateRight.getMonth(); return yearDiff * 12 + monthDiff; } /** * @name differenceInCalendarYears * @category Year Helpers * @summary Get the number of calendar years between the given dates. * * @description * Get the number of calendar years between the given dates. * * ### v2.0.0 breaking changes: * * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes). * * @param {Date|Number} dateLeft - the later date * @param {Date|Number} dateRight - the earlier date * @returns {Number} the number of calendar years * @throws {TypeError} 2 arguments required * * @example * // How many calendar years are between 31 December 2013 and 11 February 2015? * var result = differenceInCalendarYears( * new Date(2015, 1, 11), * new Date(2013, 11, 31) * ) * //=> 2 */ function differenceInCalendarYears(dirtyDateLeft, dirtyDateRight) { requiredArgs(2, arguments); var dateLeft = toDate(dirtyDateLeft); var dateRight = toDate(dirtyDateRight); return dateLeft.getFullYear() - dateRight.getFullYear(); } // for accurate equality comparisons of UTC timestamps that end up // having the same representation in local time, e.g. one hour before // DST ends vs. the instant that DST ends. function compareLocalAsc(dateLeft, dateRight) { var diff = dateLeft.getFullYear() - dateRight.getFullYear() || dateLeft.getMonth() - dateRight.getMonth() || dateLeft.getDate() - dateRight.getDate() || dateLeft.getHours() - dateRight.getHours() || dateLeft.getMinutes() - dateRight.getMinutes() || dateLeft.getSeconds() - dateRight.getSeconds() || dateLeft.getMilliseconds() - dateRight.getMilliseconds(); if (diff < 0) { return -1; } else if (diff > 0) { return 1; // Return 0 if diff is 0; return NaN if diff is NaN } else { return diff; } } /** * @name differenceInDays * @category Day Helpers * @summary Get the number of full days between the given dates. * * @description * Get the number of full day periods between two dates. Fractional days are * truncated towards zero. * * One "full day" is the distance between a local time in one day to the same * local time on the next or previous day. A full day can sometimes be less than * or more than 24 hours if a daylight savings change happens between two dates. * * To ignore DST and only measure exact 24-hour periods, use this instead: * `Math.floor(differenceInHours(dateLeft, dateRight)/24)|0`. * * * ### v2.0.0 breaking changes: * * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes). * * @param {Date|Number} dateLeft - the later date * @param {Date|Number} dateRight - the earlier date * @returns {Number} the number of full days according to the local timezone * @throws {TypeError} 2 arguments required * * @example * // How many full days are between * // 2 July 2011 23:00:00 and 2 July 2012 00:00:00? * var result = differenceInDays( * new Date(2012, 6, 2, 0, 0), * new Date(2011, 6, 2, 23, 0) * ) * //=> 365 * // How many full days are between * // 2 July 2011 23:59:00 and 3 July 2011 00:01:00? * var result = differenceInDays( * new Date(2011, 6, 3, 0, 1), * new Date(2011, 6, 2, 23, 59) * ) * //=> 0 * // How many full days are between * // 1 March 2020 0:00 and 1 June 2020 0:00 ? * // Note: because local time is used, the * // result will always be 92 days, even in * // time zones where DST starts and the * // period has only 92*24-1 hours. * var result = differenceInDays( * new Date(2020, 5, 1), * new Date(2020, 2, 1) * ) //=> 92 */ function differenceInDays(dirtyDateLeft, dirtyDateRight) { requiredArgs(2, arguments); var dateLeft = toDate(dirtyDateLeft); var dateRight = toDate(dirtyDateRight); var sign = compareLocalAsc(dateLeft, dateRight); var difference = Math.abs(differenceInCalendarDays(dateLeft, dateRight)); dateLeft.setDate(dateLeft.getDate() - sign * difference); // Math.abs(diff in full days - diff in calendar days) === 1 if last calendar day is not full // If so, result must be decreased by 1 in absolute value var isLastDayNotFull = compareLocalAsc(dateLeft, dateRight) === -sign; var result = sign * (difference - isLastDayNotFull); // Prevent negative zero return result === 0 ? 0 : result; } /** * @name differenceInMilliseconds * @category Millisecond Helpers * @summary Get the number of milliseconds between the given dates. * * @description * Get the number of milliseconds between the given dates. * * ### v2.0.0 breaking changes: * * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes). * * @param {Date|Number} dateLeft - the later date * @param {Date|Number} dateRight - the earlier date * @returns {Number} the number of milliseconds * @throws {TypeError} 2 arguments required * * @example * // How many milliseconds are between * // 2 July 2014 12:30:20.600 and 2 July 2014 12:30:21.700? * var result = differenceInMilliseconds( * new Date(2014, 6, 2, 12, 30, 21, 700), * new Date(2014, 6, 2, 12, 30, 20, 600) * ) * //=> 1100 */ function differenceInMilliseconds(dirtyDateLeft, dirtyDateRight) { requiredArgs(2, arguments); var dateLeft = toDate(dirtyDateLeft); var dateRight = toDate(dirtyDateRight); return dateLeft.getTime() - dateRight.getTime(); } var MILLISECONDS_IN_HOUR$2 = 3600000; /** * @name differenceInHours * @category Hour Helpers * @summary Get the number of hours between the given dates. * * @description * Get the number of hours between the given dates. * * ### v2.0.0 breaking changes: * * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes). * * @param {Date|Number} dateLeft - the later date * @param {Date|Number} dateRight - the earlier date * @returns {Number} the number of hours * @throws {TypeError} 2 arguments required * * @example * // How many hours are between 2 July 2014 06:50:00 and 2 July 2014 19:00:00? * var result = differenceInHours( * new Date(2014, 6, 2, 19, 0), * new Date(2014, 6, 2, 6, 50) * ) * //=> 12 */ function differenceInHours(dirtyDateLeft, dirtyDateRight) { requiredArgs(2, arguments); var diff = differenceInMilliseconds(dirtyDateLeft, dirtyDateRight) / MILLISECONDS_IN_HOUR$2; return diff > 0 ? Math.floor(diff) : Math.ceil(diff); } var MILLISECONDS_IN_MINUTE$2 = 60000; /** * @name differenceInMinutes * @category Minute Helpers * @summary Get the number of minutes between the given dates. * * @description * Get the signed number of full (rounded towards 0) minutes between the given dates. * * ### v2.0.0 breaking changes: * * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes). * * @param {Date|Number} dateLeft - the later date * @param {Date|Number} dateRight - the earlier date * @returns {Number} the number of minutes * @throws {TypeError} 2 arguments required * * @example * // How many minutes are between 2 July 2014 12:07:59 and 2 July 2014 12:20:00? * var result = differenceInMinutes( * new Date(2014, 6, 2, 12, 20, 0), * new Date(2014, 6, 2, 12, 7, 59) * ) * //=> 12 * * @example * // How many minutes are from 10:01:59 to 10:00:00 * var result = differenceInMinutes( * new Date(2000, 0, 1, 10, 0, 0), * new Date(2000, 0, 1, 10, 1, 59) * ) * //=> -1 */ function differenceInMinutes(dirtyDateLeft, dirtyDateRight) { requiredArgs(2, arguments); var diff = differenceInMilliseconds(dirtyDateLeft, dirtyDateRight) / MILLISECONDS_IN_MINUTE$2; return diff > 0 ? Math.floor(diff) : Math.ceil(diff); } /** * @name endOfDay * @category Day Helpers * @summary Return the end of a day for the given date. * * @description * Return the end of a day for the given date. * The result will be in the local timezone. * * ### v2.0.0 breaking changes: * * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes). * * @param {Date|Number} date - the original date * @returns {Date} the end of a day * @throws {TypeError} 1 argument required * * @example * // The end of a day for 2 September 2014 11:55:00: * const result = endOfDay(new Date(2014, 8, 2, 11, 55, 0)) * //=> Tue Sep 02 2014 23:59:59.999 */ function endOfDay(dirtyDate) { requiredArgs(1, arguments); var date = toDate(dirtyDate); date.setHours(23, 59, 59, 999); return date; } /** * @name endOfMonth * @category Month Helpers * @summary Return the end of a month for the given date. * * @description * Return the end of a month for the given date. * The result will be in the local timezone. * * ### v2.0.0 breaking changes: * * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes). * * @param {Date|Number} date - the original date * @returns {Date} the end of a month * @throws {TypeError} 1 argument required * * @example * // The end of a month for 2 September 2014 11:55:00: * const result = endOfMonth(new Date(2014, 8, 2, 11, 55, 0)) * //=> Tue Sep 30 2014 23:59:59.999 */ function endOfMonth(dirtyDate) { requiredArgs(1, arguments); var date = toDate(dirtyDate); var month = date.getMonth(); date.setFullYear(date.getFullYear(), month + 1, 0); date.setHours(23, 59, 59, 999); return date; } /** * @name isLastDayOfMonth * @category Month Helpers * @summary Is the given date the last day of a month? * * @description * Is the given date the last day of a month? * * ### v2.0.0 breaking changes: * * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes). * * @param {Date|Number} date - the date to check * @returns {Boolean} the date is the last day of a month * @throws {TypeError} 1 argument required * * @example * // Is 28 February 2014 the last day of a month? * var result = isLastDayOfMonth(new Date(2014, 1, 28)) * //=> true */ function isLastDayOfMonth(dirtyDate) { requiredArgs(1, arguments); var date = toDate(dirtyDate); return endOfDay(date).getTime() === endOfMonth(date).getTime(); } /** * @name differenceInMonths * @category Month Helpers * @summary Get the number of full months between the given dates. * * @description * Get the number of full months between the given dates. * * ### v2.0.0 breaking changes: * * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes). * * @param {Date|Number} dateLeft - the later date * @param {Date|Number} dateRight - the earlier date * @returns {Number} the number of full months * @throws {TypeError} 2 arguments required * * @example * // How many full months are between 31 January 2014 and 1 September 2014? * var result = differenceInMonths(new Date(2014, 8, 1), new Date(2014, 0, 31)) * //=> 7 */ function differenceInMonths(dirtyDateLeft, dirtyDateRight) { requiredArgs(2, arguments); var dateLeft = toDate(dirtyDateLeft); var dateRight = toDate(dirtyDateRight); var sign = compareAsc(dateLeft, dateRight); var difference = Math.abs(differenceInCalendarMonths(dateLeft, dateRight)); var result; // Check for the difference of less than month if (difference < 1) { result = 0; } else { if (dateLeft.getMonth() === 1 && dateLeft.getDate() > 27) { // This will check if the date is end of Feb and assign a higher end of month date // to compare it with Jan dateLeft.setDate(30); } dateLeft.setMonth(dateLeft.getMonth() - sign * difference); // Math.abs(diff in full months - diff in calendar months) === 1 if last calendar month is not full // If so, result must be decreased by 1 in absolute value var isLastMonthNotFull = compareAsc(dateLeft, dateRight) === -sign; // Check for cases of one full calendar month if (isLastDayOfMonth(toDate(dirtyDateLeft)) && difference === 1 && compareAsc(dirtyDateLeft, dateRight) === 1) { isLastMonthNotFull = false; } result = sign * (difference - isLastMonthNotFull); } // Prevent negative zero return result === 0 ? 0 : result; } /** * @name differenceInQuarters * @category Quarter Helpers * @summary Get the number of full quarters between the given dates. * * @description * Get the number of full quarters between the given dates. * * ### v2.0.0 breaking changes: * * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes). * * @param {Date|Number} dateLeft - the later date * @param {Date|Number} dateRight - the earlier date * @returns {Number} the number of full quarters * @throws {TypeError} 2 arguments required * * @example * // How many full quarters are between 31 December 2013 and 2 July 2014? * var result = differenceInQuarters(new Date(2014, 6, 2), new Date(2013, 11, 31)) * //=> 2 */ function differenceInQuarters(dirtyDateLeft, dirtyDateRight) { requiredArgs(2, arguments); var diff = differenceInMonths(dirtyDateLeft, dirtyDateRight) / 3; return diff > 0 ? Math.floor(diff) : Math.ceil(diff); } /** * @name differenceInSeconds * @category Second Helpers * @summary Get the number of seconds between the given dates. * * @description * Get the number of seconds between the given dates. * * ### v2.0.0 breaking changes: * * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes). * * @param {Date|Number} dateLeft - the later date * @param {Date|Number} dateRight - the earlier date * @returns {Number} the number of seconds * @throws {TypeError} 2 arguments required * * @example * // How many seconds are between * // 2 July 2014 12:30:07.999 and 2 July 2014 12:30:20.000? * var result = differenceInSeconds( * new Date(2014, 6, 2, 12, 30, 20, 0), * new Date(2014, 6, 2, 12, 30, 7, 999) * ) * //=> 12 */ function differenceInSeconds(dirtyDateLeft, dirtyDateRight) { requiredArgs(2, arguments); var diff = differenceInMilliseconds(dirtyDateLeft, dirtyDateRight) / 1000; return diff > 0 ? Math.floor(diff) : Math.ceil(diff); } /** * @name differenceInWeeks * @category Week Helpers * @summary Get the number of full weeks between the given dates. * * @description * Get the number of full weeks between two dates. Fractional weeks are * truncated towards zero. * * One "full week" is the distance between a local time in one day to the same * local time 7 days earlier or later. A full week can sometimes be less than * or more than 7*24 hours if a daylight savings change happens between two dates. * * To ignore DST and only measure exact 7*24-hour periods, use this instead: * `Math.floor(differenceInHours(dateLeft, dateRight)/(7*24))|0`. * * * ### v2.0.0 breaking changes: * * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes). * * @param {Date|Number} dateLeft - the later date * @param {Date|Number} dateRight - the earlier date * @returns {Number} the number of full weeks * @throws {TypeError} 2 arguments required * * @example * // How many full weeks are between 5 July 2014 and 20 July 2014? * var result = differenceInWeeks(new Date(2014, 6, 20), new Date(2014, 6, 5)) * //=> 2 * * // How many full weeks are between * // 1 March 2020 0:00 and 6 June 2020 0:00 ? * // Note: because local time is used, the * // result will always be 8 weeks (54 days), * // even if DST starts and the period has * // only 54*24-1 hours. * var result = differenceInWeeks( * new Date(2020, 5, 1), * new Date(2020, 2, 6) * ) * //=> 8 */ function differenceInWeeks(dirtyDateLeft, dirtyDateRight) { requiredArgs(2, arguments); var diff = differenceInDays(dirtyDateLeft, dirtyDateRight) / 7; return diff > 0 ? Math.floor(diff) : Math.ceil(diff); } /** * @name differenceInYears * @category Year Helpers * @summary Get the number of full years between the given dates. * * @description * Get the number of full years between the given dates. * * ### v2.0.0 breaking changes: * * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes). * * @param {Date|Number} dateLeft - the later date * @param {Date|Number} dateRight - the earlier date * @returns {Number} the number of full years * @throws {TypeError} 2 arguments required * * @example * // How many full years are between 31 December 2013 and 11 February 2015? * var result = differenceInYears(new Date(2015, 1, 11), new Date(2013, 11, 31)) * //=> 1 */ function differenceInYears(dirtyDateLeft, dirtyDateRight) { requiredArgs(2, arguments); var dateLeft = toDate(dirtyDateLeft); var dateRight = toDate(dirtyDateRight); var sign = compareAsc(dateLeft, dateRight); var difference = Math.abs(differenceInCalendarYears(dateLeft, dateRight)); // Set both dates to a valid leap year for accurate comparison when dealing // with leap days dateLeft.setFullYear('1584'); dateRight.setFullYear('1584'); // Math.abs(diff in full years - diff in calendar years) === 1 if last calendar year is not full // If so, result must be decreased by 1 in absolute value var isLastYearNotFull = compareAsc(dateLeft, dateRight) === -sign; var result = sign * (difference - isLastYearNotFull); // Prevent negative zero return result === 0 ? 0 : result; } /** * @name startOfQuarter * @category Quarter Helpers * @summary Return the start of a year quarter for the given date. * * @description * Return the start of a year quarter for the given date. * The result will be in the local timezone. * * ### v2.0.0 breaking changes: * * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes). * * @param {Date|Number} date - the original date * @returns {Date} the start of a quarter * @throws {TypeError} 1 argument required * * @example * // The start of a quarter for 2 September 2014 11:55:00: * const result = startOfQuarter(new Date(2014, 8, 2, 11, 55, 0)) * //=> Tue Jul 01 2014 00:00:00 */ function startOfQuarter(dirtyDate) { requiredArgs(1, arguments); var date = toDate(dirtyDate); var currentMonth = date.getMonth(); var month = currentMonth - currentMonth % 3; date.setMonth(month, 1); date.setHours(0, 0, 0, 0); return date; } /** * @name startOfMonth * @category Month Helpers * @summary Return the start of a month for the given date. * * @description * Return the start of a month for the given date. * The result will be in the local timezone. * * ### v2.0.0 breaking changes: * * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes). * * @param {Date|Number} date - the original date * @returns {Date} the start of a month * @throws {TypeError} 1 argument required * * @example * // The start of a month for 2 September 2014 11:55:00: * const result = startOfMonth(new Date(2014, 8, 2, 11, 55, 0)) * //=> Mon Sep 01 2014 00:00:00 */ function startOfMonth(dirtyDate) { requiredArgs(1, arguments); var date = toDate(dirtyDate); date.setDate(1); date.setHours(0, 0, 0, 0); return date; } /** * @name startOfYear * @category Year Helpers * @summary Return the start of a year for the given date. * * @description * Return the start of a year for the given date. * The result will be in the local timezone. * * ### v2.0.0 breaking changes: * * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes). * * @param {Date|Number} date - the original date * @returns {Date} the start of a year * @throws {TypeError} 1 argument required * * @example * // The start of a year for 2 September 2014 11:55:00: * const result = startOfYear(new Date(2014, 8, 2, 11, 55, 00)) * //=> Wed Jan 01 2014 00:00:00 */ function startOfYear(dirtyDate) { requiredArgs(1, arguments); var cleanDate = toDate(dirtyDate); var date = new Date(0); date.setFullYear(cleanDate.getFullYear(), 0, 1); date.setHours(0, 0, 0, 0); return date; } /** * @name endOfYear * @category Year Helpers * @summary Return the end of a year for the given date. * * @description * Return the end of a year for the given date. * The result will be in the local timezone. * * ### v2.0.0 breaking changes: * * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes). * * @param {Date|Number} date - the original date * @returns {Date} the end of a year * @throws {TypeError} 1 argument required * * @example * // The end of a year for 2 September 2014 11:55:00: * var result = endOfYear(new Date(2014, 8, 2, 11, 55, 00)) * //=> Wed Dec 31 2014 23:59:59.999 */ function endOfYear(dirtyDate) { requiredArgs(1, arguments); var date = toDate(dirtyDate); var year = date.getFullYear(); date.setFullYear(year + 1, 0, 0); date.setHours(23, 59, 59, 999); return date; } /** * @name endOfHour * @category Hour Helpers * @summary Return the end of an hour for the given date. * * @description * Return the end of an hour for the given date. * The result will be in the local timezone. * * ### v2.0.0 breaking changes: * * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes). * * @param {Date|Number} date - the original date * @returns {Date} the end of an hour * @throws {TypeError} 1 argument required * * @example * // The end of an hour for 2 September 2014 11:55:00: * const result = endOfHour(new Date(2014, 8, 2, 11, 55)) * //=> Tue Sep 02 2014 11:59:59.999 */ function endOfHour(dirtyDate) { requiredArgs(1, arguments); var date = toDate(dirtyDate); date.setMinutes(59, 59, 999); return date; } /** * @name endOfWeek * @category Week Helpers * @summary Return the end of a week for the given date. * * @description * Return the end of a week for the given date. * The result will be in the local timezone. * * ### v2.0.0 breaking changes: * * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes). * * @param {Date|Number} date - the original date * @param {Object} [options] - an object with options. * @param {Locale} [options.locale=defaultLocale] - the locale object. See [Locale]{@link https://date-fns.org/docs/Locale} * @param {0|1|2|3|4|5|6} [options.weekStartsOn=0] - the index of the first day of the week (0 - Sunday) * @returns {Date} the end of a week * @throws {TypeError} 1 argument required * @throws {RangeError} `options.weekStartsOn` must be between 0 and 6 * * @example * // The end of a week for 2 September 2014 11:55:00: * const result = endOfWeek(new Date(2014, 8, 2, 11, 55, 0)) * //=> Sat Sep 06 2014 23:59:59.999 * * @example * // If the week starts on Monday, the end of the week for 2 September 2014 11:55:00: * const result = endOfWeek(new Date(2014, 8, 2, 11, 55, 0), { weekStartsOn: 1 }) * //=> Sun Sep 07 2014 23:59:59.999 */ function endOfWeek(dirtyDate, dirtyOptions) { requiredArgs(1, arguments); var options = dirtyOptions || {}; var locale = options.locale; var localeWeekStartsOn = locale && locale.options && locale.options.weekStartsOn; var defaultWeekStartsOn = localeWeekStartsOn == null ? 0 : toInteger(localeWeekStartsOn); var weekStartsOn = options.weekStartsOn == null ? defaultWeekStartsOn : toInteger(options.weekStartsOn); // Test if weekStartsOn is between 0 and 6 _and_ is not NaN if (!(weekStartsOn >= 0 && weekStartsOn <= 6)) { throw new RangeError('weekStartsOn must be between 0 and 6 inclusively'); } var date = toDate(dirtyDate); var day = date.getDay(); var diff = (day < weekStartsOn ? -7 : 0) + 6 - (day - weekStartsOn); date.setDate(date.getDate() + diff); date.setHours(23, 59, 59, 999); return date; } /** * @name endOfMinute * @category Minute Helpers * @summary Return the end of a minute for the given date. * * @description * Return the end of a minute for the given date. * The result will be in the local timezone. * * ### v2.0.0 breaking changes: * * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes). * * @param {Date|Number} date - the original date * @returns {Date} the end of a minute * @throws {TypeError} 1 argument required * * @example * // The end of a minute for 1 December 2014 22:15:45.400: * const result = endOfMinute(new Date(2014, 11, 1, 22, 15, 45, 400)) * //=> Mon Dec 01 2014 22:15:59.999 */ function endOfMinute(dirtyDate) { requiredArgs(1, arguments); var date = toDate(dirtyDate); date.setSeconds(59, 999); return date; } /** * @name endOfQuart