date-fns
- Version 4.1.0
- Published
- 22.6 MB
- No dependencies
- MIT license
Install
npm i date-fns
yarn add date-fns
pnpm add date-fns
Overview
Modern JavaScript date utility library
Index
Variables
Functions
- add()
- addBusinessDays()
- addDays()
- addHours()
- addISOWeekYears()
- addMilliseconds()
- addMinutes()
- addMonths()
- addQuarters()
- addSeconds()
- addWeeks()
- addYears()
- areIntervalsOverlapping()
- clamp()
- closestIndexTo()
- closestTo()
- compareAsc()
- compareDesc()
- constructFrom()
- constructNow()
- daysToWeeks()
- differenceInBusinessDays()
- differenceInCalendarDays()
- differenceInCalendarISOWeeks()
- differenceInCalendarISOWeekYears()
- differenceInCalendarMonths()
- differenceInCalendarQuarters()
- differenceInCalendarWeeks()
- differenceInCalendarYears()
- differenceInDays()
- differenceInHours()
- differenceInISOWeekYears()
- differenceInMilliseconds()
- differenceInMinutes()
- differenceInMonths()
- differenceInQuarters()
- differenceInSeconds()
- differenceInWeeks()
- differenceInYears()
- eachDayOfInterval()
- eachHourOfInterval()
- eachMinuteOfInterval()
- eachMonthOfInterval()
- eachQuarterOfInterval()
- eachWeekendOfInterval()
- eachWeekendOfMonth()
- eachWeekendOfYear()
- eachWeekOfInterval()
- eachYearOfInterval()
- endOfDay()
- endOfDecade()
- endOfHour()
- endOfISOWeek()
- endOfISOWeekYear()
- endOfMinute()
- endOfMonth()
- endOfQuarter()
- endOfSecond()
- endOfToday()
- endOfTomorrow()
- endOfWeek()
- endOfYear()
- endOfYesterday()
- format()
- formatDate()
- formatDistance()
- formatDistanceStrict()
- formatDistanceToNow()
- formatDistanceToNowStrict()
- formatDuration()
- formatISO()
- formatISO9075()
- formatISODuration()
- formatRelative()
- formatRFC3339()
- formatRFC7231()
- fromUnixTime()
- getDate()
- getDay()
- getDayOfYear()
- getDaysInMonth()
- getDaysInYear()
- getDecade()
- getDefaultOptions()
- getHours()
- getISODay()
- getISOWeek()
- getISOWeeksInYear()
- getISOWeekYear()
- getMilliseconds()
- getMinutes()
- getMonth()
- getOverlappingDaysInIntervals()
- getQuarter()
- getSeconds()
- getTime()
- getUnixTime()
- getWeek()
- getWeekOfMonth()
- getWeeksInMonth()
- getWeekYear()
- getYear()
- hoursToMilliseconds()
- hoursToMinutes()
- hoursToSeconds()
- interval()
- intervalToDuration()
- intlFormat()
- intlFormatDistance()
- isAfter()
- isBefore()
- isDate()
- isEqual()
- isExists()
- isFirstDayOfMonth()
- isFriday()
- isFuture()
- isLastDayOfMonth()
- isLeapYear()
- isMatch()
- isMonday()
- isPast()
- isSameDay()
- isSameHour()
- isSameISOWeek()
- isSameISOWeekYear()
- isSameMinute()
- isSameMonth()
- isSameQuarter()
- isSameSecond()
- isSameWeek()
- isSameYear()
- isSaturday()
- isSunday()
- isThisHour()
- isThisISOWeek()
- isThisMinute()
- isThisMonth()
- isThisQuarter()
- isThisSecond()
- isThisWeek()
- isThisYear()
- isThursday()
- isToday()
- isTomorrow()
- isTuesday()
- isValid()
- isWednesday()
- isWeekend()
- isWithinInterval()
- isYesterday()
- lastDayOfDecade()
- lastDayOfISOWeek()
- lastDayOfISOWeekYear()
- lastDayOfMonth()
- lastDayOfQuarter()
- lastDayOfWeek()
- lastDayOfYear()
- lightFormat()
- max()
- milliseconds()
- millisecondsToHours()
- millisecondsToMinutes()
- millisecondsToSeconds()
- min()
- minutesToHours()
- minutesToMilliseconds()
- minutesToSeconds()
- monthsToQuarters()
- monthsToYears()
- nextDay()
- nextFriday()
- nextMonday()
- nextSaturday()
- nextSunday()
- nextThursday()
- nextTuesday()
- nextWednesday()
- parse()
- parseISO()
- parseJSON()
- previousDay()
- previousFriday()
- previousMonday()
- previousSaturday()
- previousSunday()
- previousThursday()
- previousTuesday()
- previousWednesday()
- quartersToMonths()
- quartersToYears()
- roundToNearestHours()
- roundToNearestMinutes()
- secondsToHours()
- secondsToMilliseconds()
- secondsToMinutes()
- set()
- setDate()
- setDay()
- setDayOfYear()
- setDefaultOptions()
- setHours()
- setISODay()
- setISOWeek()
- setISOWeekYear()
- setMilliseconds()
- setMinutes()
- setMonth()
- setQuarter()
- setSeconds()
- setWeek()
- setWeekYear()
- setYear()
- startOfDay()
- startOfDecade()
- startOfHour()
- startOfISOWeek()
- startOfISOWeekYear()
- startOfMinute()
- startOfMonth()
- startOfQuarter()
- startOfSecond()
- startOfToday()
- startOfTomorrow()
- startOfWeek()
- startOfWeekYear()
- startOfYear()
- startOfYesterday()
- sub()
- subBusinessDays()
- subDays()
- subHours()
- subISOWeekYears()
- subMilliseconds()
- subMinutes()
- subMonths()
- subQuarters()
- subSeconds()
- subWeeks()
- subYears()
- toDate()
- transpose()
- weeksToDays()
- yearsToDays()
- yearsToMonths()
- yearsToQuarters()
Interfaces
Type Aliases
- ClampResult
- ClosestToResult
- ContextFn
- DateArg
- Day
- DurationUnit
- EachDayOfIntervalResult
- EachHourOfIntervalResult
- EachMinuteOfIntervalResult
- EachMonthOfIntervalResult
- EachQuarterOfIntervalResult
- EachWeekendOfIntervalResult
- EachWeekOfIntervalResult
- EachYearOfIntervalResult
- Era
- FirstWeekContainsDate
- FormatDistanceFn
- FormatDistanceLocale
- FormatDistanceStrictUnit
- FormatDistanceToken
- FormatDistanceTokenFn
- FormatLongFn
- FormatLongWidth
- FormatRelativeFn
- FormatRelativeToken
- FormatRelativeTokenFn
- FPArity
- FPFn
- FPFnInput
- IntervalResult
- IntlFormatDistanceUnit
- IntlFormatFormatOptions
- IntlFormatLocale
- ISOStringFormat
- ISOStringRepresentation
- LocaleDayPeriod
- LocaleUnit
- LocaleUnitValue
- LocaleWidth
- LocalizeFn
- MatchFn
- MatchValueCallback
- MaybeArray
- Month
- NearestHours
- NearestMinutes
- NearestMinutesOptions
- NormalizedInterval
- Quarter
- RoundingMethod
Variables
variable formatters
const formatters: { [token: string]: Formatter };
variable lightFormatters
const lightFormatters: { y(date: Date, token: string): string; M(date: Date, token: string): string; d(date: Date, token: string): string; a(date: Date, token: string): string; h(date: Date, token: string): string; H(date: Date, token: string): string; m(date: Date, token: string): string; s(date: Date, token: string): string; S(date: Date, token: string): string;};
variable longFormatters
const longFormatters: Record<string, LongFormatter>;
variable parsers
const parsers: Record<string, Parser<any>>;
Functions
function add
add: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, duration: Duration, options?: AddOptions<ResultDate> | undefined) => ResultDate;
add Common Helpers Add the specified years, months, weeks, days, hours, minutes, and seconds to the given date.
Add the specified years, months, weeks, days, hours, minutes, and seconds to the given date.
Parameter date
The date to be changed
Parameter duration
The object with years, months, weeks, days, hours, minutes, and seconds to be added.
Parameter options
An object with options
Returns
The new date with the seconds added
Example 1
// Add the following duration to 1 September 2014, 10:19:50 const result = add(new Date(2014, 8, 1, 10, 19, 50), { years: 2, months: 9, weeks: 1, days: 7, hours: 5, minutes: 9, seconds: 30, }) //=> Thu Jun 15 2017 15:29:20
function addBusinessDays
addBusinessDays: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, amount: number, options?: AddBusinessDaysOptions<ResultDate> | undefined) => ResultDate;
addBusinessDays Day Helpers Add the specified number of business days (mon - fri) to the given date.
Add the specified number of business days (mon - fri) to the given date, ignoring weekends.
Parameter date
The date to be changed
Parameter amount
The amount of business days to be added.
Parameter options
An object with options
Returns
The new date with the business days added
Example 1
// Add 10 business days to 1 September 2014: const result = addBusinessDays(new Date(2014, 8, 1), 10) //=> Mon Sep 15 2014 00:00:00 (skipped weekend days)
function addDays
addDays: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, amount: number, options?: AddDaysOptions<ResultDate> | undefined) => ResultDate;
addDays Day Helpers Add the specified number of days to the given date.
Add the specified number of days to the given date.
Parameter date
The date to be changed
Parameter amount
The amount of days to be added.
Parameter options
An object with options
Returns
The new date with the days added
Example 1
// Add 10 days to 1 September 2014: const result = addDays(new Date(2014, 8, 1), 10) //=> Thu Sep 11 2014 00:00:00
function addHours
addHours: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, amount: number, options?: AddHoursOptions<ResultDate> | undefined) => ResultDate;
addHours Hour Helpers Add the specified number of hours to the given date.
Add the specified number of hours to the given date.
Parameter date
The date to be changed
Parameter amount
The amount of hours to be added
Parameter options
An object with options
Returns
The new date with the hours added
Example 1
// 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 addISOWeekYears
addISOWeekYears: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, amount: number, options?: AddISOWeekYearsOptions<ResultDate> | undefined) => ResultDate;
addISOWeekYears ISO Week-Numbering Year Helpers Add the specified number of ISO week-numbering years to the given date.
Add the specified number of ISO week-numbering years to the given date.
ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_date
Parameter date
The date to be changed
Parameter amount
The amount of ISO week-numbering years to be added.
Parameter options
An object with options
Returns
The new date with the ISO week-numbering years added
Example 1
// Add 5 ISO week-numbering years to 2 July 2010: const result = addISOWeekYears(new Date(2010, 6, 2), 5) //=> Fri Jun 26 2015 00:00:00
function addMilliseconds
addMilliseconds: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, amount: number, options?: AddMillisecondsOptions<ResultDate> | undefined) => ResultDate;
addMilliseconds Millisecond Helpers Add the specified number of milliseconds to the given date.
Add the specified number of milliseconds to the given date.
Parameter date
The date to be changed
Parameter amount
The amount of milliseconds to be added.
Parameter options
The options object
Returns
The new date with the milliseconds added
Example 1
// 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 addMinutes
addMinutes: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, amount: number, options?: AddMinutesOptions<ResultDate> | undefined) => ResultDate;
addMinutes Minute Helpers Add the specified number of minutes to the given date.
Add the specified number of minutes to the given date.
Parameter date
The date to be changed
Parameter amount
The amount of minutes to be added.
Parameter options
An object with options
Returns
The new date with the minutes added
Example 1
// 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 addMonths
addMonths: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, amount: number, options?: AddMonthsOptions<ResultDate> | undefined) => ResultDate;
addMonths Month Helpers Add the specified number of months to the given date.
Add the specified number of months to the given date.
Parameter date
The date to be changed
Parameter amount
The amount of months to be added.
Parameter options
The options object
Returns
The new date with the months added
Example 1
// Add 5 months to 1 September 2014: const result = addMonths(new Date(2014, 8, 1), 5) //=> Sun Feb 01 2015 00:00:00
// Add one month to 30 January 2023: const result = addMonths(new Date(2023, 0, 30), 1) //=> Tue Feb 28 2023 00:00:00
function addQuarters
addQuarters: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, amount: number, options?: AddQuartersOptions<ResultDate> | undefined) => ResultDate;
addQuarters Quarter Helpers Add the specified number of year quarters to the given date.
Add the specified number of year quarters to the given date.
Parameter date
The date to be changed
Parameter amount
The amount of quarters to be added.
Parameter options
An object with options
Returns
The new date with the quarters added
Example 1
// Add 1 quarter to 1 September 2014: const result = addQuarters(new Date(2014, 8, 1), 1) //=; Mon Dec 01 2014 00:00:00
function addSeconds
addSeconds: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, amount: number, options?: AddSecondsOptions<ResultDate> | undefined) => ResultDate;
addSeconds Second Helpers Add the specified number of seconds to the given date.
Add the specified number of seconds to the given date.
Parameter date
The date to be changed
Parameter amount
The amount of seconds to be added.
Parameter options
An object with options
Returns
The new date with the seconds added
Example 1
// 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 addWeeks
addWeeks: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, amount: number, options?: AddWeeksOptions<ResultDate> | undefined) => ResultDate;
addWeeks Week Helpers Add the specified number of weeks to the given date.
Add the specified number of weeks to the given date.
Parameter date
The date to be changed
Parameter amount
The amount of weeks to be added.
Parameter options
An object with options
Returns
The new date with the weeks added
Example 1
// Add 4 weeks to 1 September 2014: const result = addWeeks(new Date(2014, 8, 1), 4) //=> Mon Sep 29 2014 00:00:00
function addYears
addYears: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, amount: number, options?: AddYearsOptions<ResultDate> | undefined) => ResultDate;
addYears Year Helpers Add the specified number of years to the given date.
Add the specified number of years to the given date.
Parameter date
The date to be changed
Parameter amount
The amount of years to be added.
Parameter options
The options
Returns
The new date with the years added
Example 1
// Add 5 years to 1 September 2014: const result = addYears(new Date(2014, 8, 1), 5) //=> Sun Sep 01 2019 00:00:00
function areIntervalsOverlapping
areIntervalsOverlapping: ( intervalLeft: Interval, intervalRight: Interval, options?: AreIntervalsOverlappingOptions) => boolean;
areIntervalsOverlapping Interval Helpers Is the given time interval overlapping with another time interval?
Is the given time interval overlapping with another time interval? Adjacent intervals do not count as overlapping unless
inclusive
is set totrue
.Parameter intervalLeft
The first interval to compare.
Parameter intervalRight
The second interval to compare.
Parameter options
The object with options
Returns
Whether the time intervals are overlapping
Example 1
// For overlapping time intervals: areIntervalsOverlapping( { start: new Date(2014, 0, 10), end: new Date(2014, 0, 20) }, { start: new Date(2014, 0, 17), end: new Date(2014, 0, 21) } ) //=> true
Example 2
// For non-overlapping time intervals: areIntervalsOverlapping( { start: new Date(2014, 0, 10), end: new Date(2014, 0, 20) }, { start: new Date(2014, 0, 21), end: new Date(2014, 0, 22) } ) //=> false
Example 3
// For adjacent time intervals: areIntervalsOverlapping( { start: new Date(2014, 0, 10), end: new Date(2014, 0, 20) }, { start: new Date(2014, 0, 20), end: new Date(2014, 0, 30) } ) //=> false
Example 4
// Using the inclusive option: areIntervalsOverlapping( { start: new Date(2014, 0, 10), end: new Date(2014, 0, 20) }, { start: new Date(2014, 0, 20), end: new Date(2014, 0, 24) }, { inclusive: true } ) //=> true
function clamp
clamp: < DateType extends DateArg<Date>, IntervalType extends Interval<DateArg<Date>, DateArg<Date>>, Options extends ClampOptions<Date> = undefined>( date: DateType, interval: IntervalType, options?: Options) => ClampResult<DateType, IntervalType, Options>;
clamp Interval Helpers Return a date bounded by the start and the end of the given interval.
Clamps a date to the lower bound with the start of the interval and the upper bound with the end of the interval.
- When the date is less than the start of the interval, the start is returned. - When the date is greater than the end of the interval, the end is returned. - Otherwise the date is returned.
Parameter date
The date to be bounded
Parameter interval
The interval to bound to
Parameter options
An object with options
Returns
The date bounded by the start and the end of the interval
Example 1
// What is Mar 21, 2021 bounded to an interval starting at Mar 22, 2021 and ending at Apr 01, 2021 const result = clamp(new Date(2021, 2, 21), { start: new Date(2021, 2, 22), end: new Date(2021, 3, 1), }) //=> Mon Mar 22 2021 00:00:00
function closestIndexTo
closestIndexTo: ( dateToCompare: DateArg<Date> & {}, dates: Array<DateArg<Date> & {}>) => number | undefined;
closestIndexTo Common Helpers Return an index of the closest date from the array comparing to the given date.
Return an index of the closest date from the array comparing to the given date.
Parameter dateToCompare
The date to compare with
Parameter dates
The array to search
Returns
An index of the date closest to the given date or undefined if no valid value is given
Example 1
// Which date is closer to 6 September 2015? const dateToCompare = new Date(2015, 8, 6) const datesArray = [ new Date(2015, 0, 1), new Date(2016, 0, 1), new Date(2017, 0, 1) ] const result = closestIndexTo(dateToCompare, datesArray) //=> 1
function closestTo
closestTo: < DateToCompare extends DateArg<Date>, DatesType extends DateArg<Date>[], Options extends ClosestToOptions<Date> = undefined>( dateToCompare: DateToCompare, dates: DatesType, options?: Options | undefined) => ClosestToResult<DateToCompare, DatesType, Options> | undefined;
closestTo Common Helpers Return a date from the array closest to the given date.
Return a date from the array closest to the given date.
Parameter dateToCompare
The date to compare with
Parameter dates
The array to search
Returns
The date from the array closest to the given date or undefined if no valid value is given
Example 1
// Which date is closer to 6 September 2015: 1 January 2000 or 1 January 2030? const dateToCompare = new Date(2015, 8, 6) const result = closestTo(dateToCompare, [ new Date(2000, 0, 1), new Date(2030, 0, 1) ]) //=> Tue Jan 01 2030 00:00:00
function compareAsc
compareAsc: ( dateLeft: DateArg<Date> & {}, dateRight: DateArg<Date> & {}) => number;
compareAsc Common Helpers Compare the two dates and return -1, 0 or 1.
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.
Parameter dateLeft
The first date to compare
Parameter dateRight
The second date to compare
Returns
The result of the comparison
Example 1
// Compare 11 February 1987 and 10 July 1989: const result = compareAsc(new Date(1987, 1, 11), new Date(1989, 6, 10)) //=> -1
Example 2
// 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 compareDesc
compareDesc: ( dateLeft: DateArg<Date> & {}, dateRight: DateArg<Date> & {}) => number;
compareDesc Common Helpers Compare the two dates reverse chronologically and return -1, 0 or 1.
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.
Parameter dateLeft
The first date to compare
Parameter dateRight
The second date to compare
Returns
The result of the comparison
Example 1
// Compare 11 February 1987 and 10 July 1989 reverse chronologically: const result = compareDesc(new Date(1987, 1, 11), new Date(1989, 6, 10)) //=> 1
Example 2
// Sort the array of dates in reverse chronological order: const result = [ new Date(1995, 6, 2), new Date(1987, 1, 11), new Date(1989, 6, 10) ].sort(compareDesc) //=> [ // Sun Jul 02 1995 00:00:00, // Mon Jul 10 1989 00:00:00, // Wed Feb 11 1987 00:00:00 // ]
function constructFrom
constructFrom: < DateType extends Date | ConstructableDate, ResultDate extends Date = DateType>( date: DateArg<DateType> | ContextFn<ResultDate> | undefined, value: DateArg<Date> & {}) => ResultDate;
constructFrom Generic Helpers Constructs a date using the reference date and the value
The function constructs a new date using the constructor from the reference date and the given value. It helps to build generic functions that accept date extensions.
It defaults to
Date
if the passed reference date is a number or a string.Starting from v3.7.0, it allows to construct a date using
[Symbol.for("constructDateFrom")]
enabling to transfer extra properties from the reference date to the new date. It's useful for extensions like [TZDate
](https://github.com/date-fns/tz) that accept a time zone as a constructor argument.Parameter date
The reference date to take constructor from
Parameter value
The value to create the date
Returns
Date initialized using the given date and value
Example 1
import { constructFrom } from "./constructFrom/date-fns";
// A function that clones a date preserving the original type function cloneDate<DateType extends Date>(date: DateType): DateType { return constructFrom( date, // Use constructor from the given date date.getTime() // Use the date value to create a new date ); }
function constructNow
constructNow: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType> | ContextFn<ResultDate> | undefined) => ResultDate;
constructNow Generic Helpers Constructs a new current date using the passed value constructor. false
The function constructs a new current date using the constructor from the reference date. It helps to build generic functions that accept date extensions and use the current date.
It defaults to
Date
if the passed reference date is a number or a string.Parameter date
The reference date to take constructor from
Returns
Current date initialized using the given date constructor
Example 1
import { constructNow, isSameDay } from 'date-fns'
function isToday<DateType extends Date>( date: DateArg, ): boolean { // If we were to use
new Date()
directly, the function would behave // differently in different timezones and return false for the same date. return isSameDay(date, constructNow(date)); }
function daysToWeeks
daysToWeeks: (days: number) => number;
daysToWeeks Conversion Helpers Convert days to weeks.
Convert a number of days to a full number of weeks.
Parameter days
The number of days to be converted
Returns
The number of days converted in weeks
Example 1
// Convert 14 days to weeks: const result = daysToWeeks(14) //=> 2
Example 2
// It uses trunc rounding: const result = daysToWeeks(13) //=> 1
function differenceInBusinessDays
differenceInBusinessDays: ( laterDate: DateArg<Date> & {}, earlierDate: DateArg<Date> & {}, options?: DifferenceInBusinessDaysOptions | undefined) => number;
differenceInBusinessDays Day Helpers Get the number of business days between the given dates.
Get the number of business day periods between the given dates. Business days being days that aren't in the weekend. Like
differenceInCalendarDays
, the function removes the times from the dates before calculating the difference.Parameter laterDate
The later date
Parameter earlierDate
The earlier date
Parameter options
An object with options
Returns
The number of business days
Example 1
// How many business days are between // 10 January 2014 and 20 July 2014? const result = differenceInBusinessDays( new Date(2014, 6, 20), new Date(2014, 0, 10) ) //=> 136
// How many business days are between // 30 November 2021 and 1 November 2021? const result = differenceInBusinessDays( new Date(2021, 10, 30), new Date(2021, 10, 1) ) //=> 21
// How many business days are between // 1 November 2021 and 1 December 2021? const result = differenceInBusinessDays( new Date(2021, 10, 1), new Date(2021, 11, 1) ) //=> -22
// How many business days are between // 1 November 2021 and 1 November 2021 ? const result = differenceInBusinessDays( new Date(2021, 10, 1), new Date(2021, 10, 1) ) //=> 0
function differenceInCalendarDays
differenceInCalendarDays: ( laterDate: DateArg<Date> & {}, earlierDate: DateArg<Date> & {}, options?: DifferenceInCalendarDaysOptions | undefined) => number;
differenceInCalendarDays Day Helpers Get the number of calendar days between the given dates.
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.
Parameter laterDate
The later date
Parameter earlierDate
The earlier date
Parameter options
The options object
Returns
The number of calendar days
Example 1
// How many calendar days are between // 2 July 2011 23:00:00 and 2 July 2012 00:00:00? const 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? const result = differenceInCalendarDays( new Date(2011, 6, 3, 0, 1), new Date(2011, 6, 2, 23, 59) ) //=> 1
function differenceInCalendarISOWeeks
differenceInCalendarISOWeeks: ( laterDate: DateArg<Date> & {}, earlierDate: DateArg<Date> & {}, options?: DifferenceInCalendarISOWeeksOptions | undefined) => number;
differenceInCalendarISOWeeks ISO Week Helpers Get the number of calendar ISO weeks between the given dates.
Get the number of calendar ISO weeks between the given dates.
ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_date
Parameter laterDate
The later date
Parameter earlierDate
The earlier date
Parameter options
An object with options
Returns
The number of calendar ISO weeks
Example 1
// How many calendar ISO weeks are between 6 July 2014 and 21 July 2014? const result = differenceInCalendarISOWeeks( new Date(2014, 6, 21), new Date(2014, 6, 6), ); //=> 3
function differenceInCalendarISOWeekYears
differenceInCalendarISOWeekYears: ( laterDate: DateArg<Date> & {}, earlierDate: DateArg<Date> & {}, options?: DifferenceInCalendarISOWeekYearsOptions | undefined) => number;
differenceInCalendarISOWeekYears ISO Week-Numbering Year Helpers Get the number of calendar ISO week-numbering years between the given dates.
Get the number of calendar ISO week-numbering years between the given dates.
ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_date
Parameter laterDate
The later date
Parameter earlierDate
The earlier date
Parameter options
An object with options
Returns
The number of calendar ISO week-numbering years
Example 1
// How many calendar ISO week-numbering years are 1 January 2010 and 1 January 2012? const result = differenceInCalendarISOWeekYears( new Date(2012, 0, 1), new Date(2010, 0, 1) ) //=> 2
function differenceInCalendarMonths
differenceInCalendarMonths: ( laterDate: DateArg<Date> & {}, earlierDate: DateArg<Date> & {}, options?: DifferenceInCalendarMonthsOptions | undefined) => number;
differenceInCalendarMonths Month Helpers Get the number of calendar months between the given dates.
Get the number of calendar months between the given dates.
Parameter laterDate
The later date
Parameter earlierDate
The earlier date
Parameter options
An object with options
Returns
The number of calendar months
Example 1
// How many calendar months are between 31 January 2014 and 1 September 2014? const result = differenceInCalendarMonths( new Date(2014, 8, 1), new Date(2014, 0, 31) ) //=> 8
function differenceInCalendarQuarters
differenceInCalendarQuarters: ( laterDate: DateArg<Date> & {}, earlierDate: DateArg<Date> & {}, options?: DifferenceInCalendarQuartersOptions | undefined) => number;
differenceInCalendarQuarters Quarter Helpers Get the number of calendar quarters between the given dates.
Get the number of calendar quarters between the given dates.
Parameter laterDate
The later date
Parameter earlierDate
The earlier date
Parameter options
An object with options
Returns
The number of calendar quarters
Example 1
// How many calendar quarters are between 31 December 2013 and 2 July 2014? const result = differenceInCalendarQuarters( new Date(2014, 6, 2), new Date(2013, 11, 31) ) //=> 3
function differenceInCalendarWeeks
differenceInCalendarWeeks: ( laterDate: DateArg<Date> & {}, earlierDate: DateArg<Date> & {}, options?: DifferenceInCalendarWeeksOptions | undefined) => number;
differenceInCalendarWeeks Week Helpers Get the number of calendar weeks between the given dates.
Get the number of calendar weeks between the given dates.
Parameter laterDate
The later date
Parameter earlierDate
The earlier date
Parameter options
An object with options.
Returns
The number of calendar weeks
Example 1
// How many calendar weeks are between 5 July 2014 and 20 July 2014? const result = differenceInCalendarWeeks( new Date(2014, 6, 20), new Date(2014, 6, 5) ) //=> 3
Example 2
// If the week starts on Monday, // how many calendar weeks are between 5 July 2014 and 20 July 2014? const result = differenceInCalendarWeeks( new Date(2014, 6, 20), new Date(2014, 6, 5), { weekStartsOn: 1 } ) //=> 2
function differenceInCalendarYears
differenceInCalendarYears: ( laterDate: DateArg<Date> & {}, earlierDate: DateArg<Date> & {}, options?: DifferenceInCalendarYearsOptions | undefined) => number;
differenceInCalendarYears Year Helpers Get the number of calendar years between the given dates.
Get the number of calendar years between the given dates.
Parameter laterDate
The later date
Parameter earlierDate
The earlier date
Parameter options
An object with options
Returns
The number of calendar years
Example 1
// How many calendar years are between 31 December 2013 and 11 February 2015? const result = differenceInCalendarYears( new Date(2015, 1, 11), new Date(2013, 11, 31) ); //=> 2
function differenceInDays
differenceInDays: ( laterDate: DateArg<Date> & {}, earlierDate: DateArg<Date> & {}, options?: DifferenceInDaysOptions | undefined) => number;
differenceInDays Day Helpers Get the number of full days between the given dates.
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.trunc(differenceInHours(dateLeft, dateRight)/24)|0
.Parameter laterDate
The later date
Parameter earlierDate
The earlier date
Parameter options
An object with options
Returns
The number of full days according to the local timezone
Example 1
// How many full days are between // 2 July 2011 23:00:00 and 2 July 2012 00:00:00? const result = differenceInDays( new Date(2012, 6, 2, 0, 0), new Date(2011, 6, 2, 23, 0) ) //=> 365
Example 2
// How many full days are between // 2 July 2011 23:59:00 and 3 July 2011 00:01:00? const result = differenceInDays( new Date(2011, 6, 3, 0, 1), new Date(2011, 6, 2, 23, 59) ) //=> 0
Example 3
// 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. const result = differenceInDays( new Date(2020, 5, 1), new Date(2020, 2, 1) ) //=> 92
function differenceInHours
differenceInHours: ( laterDate: DateArg<Date> & {}, earlierDate: DateArg<Date> & {}, options?: DifferenceInHoursOptions) => number;
differenceInHours Hour Helpers Get the number of hours between the given dates.
Get the number of hours between the given dates.
Parameter laterDate
The later date
Parameter earlierDate
The earlier date
Parameter options
An object with options.
Returns
The number of hours
Example 1
// How many hours are between 2 July 2014 06:50:00 and 2 July 2014 19:00:00? const result = differenceInHours( new Date(2014, 6, 2, 19, 0), new Date(2014, 6, 2, 6, 50) ) //=> 12
function differenceInISOWeekYears
differenceInISOWeekYears: ( laterDate: DateArg<Date> & {}, earlierDate: DateArg<Date> & {}, options?: DifferenceInISOWeekYearsOptions | undefined) => number;
differenceInISOWeekYears ISO Week-Numbering Year Helpers Get the number of full ISO week-numbering years between the given dates.
Get the number of full ISO week-numbering years between the given dates.
ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_date
Parameter laterDate
The later date
Parameter earlierDate
The earlier date
Parameter options
The options
Returns
The number of full ISO week-numbering years
Example 1
// How many full ISO week-numbering years are between 1 January 2010 and 1 January 2012? const result = differenceInISOWeekYears( new Date(2012, 0, 1), new Date(2010, 0, 1) ) // => 1
function differenceInMilliseconds
differenceInMilliseconds: ( laterDate: DateArg<Date> & {}, earlierDate: DateArg<Date> & {}) => number;
differenceInMilliseconds Millisecond Helpers Get the number of milliseconds between the given dates.
Get the number of milliseconds between the given dates.
Parameter laterDate
The later date
Parameter earlierDate
The earlier date
Returns
The number of milliseconds
Example 1
// How many milliseconds are between // 2 July 2014 12:30:20.600 and 2 July 2014 12:30:21.700? const result = differenceInMilliseconds( new Date(2014, 6, 2, 12, 30, 21, 700), new Date(2014, 6, 2, 12, 30, 20, 600) ) //=> 1100
function differenceInMinutes
differenceInMinutes: ( dateLeft: DateArg<Date> & {}, dateRight: DateArg<Date> & {}, options?: DifferenceInMinutesOptions) => number;
differenceInMinutes Minute Helpers Get the number of minutes between the given dates.
Get the signed number of full (rounded towards 0) minutes between the given dates.
Parameter dateLeft
The later date
Parameter dateRight
The earlier date
Parameter options
An object with options.
Returns
The number of minutes
Example 1
// How many minutes are between 2 July 2014 12:07:59 and 2 July 2014 12:20:00? const result = differenceInMinutes( new Date(2014, 6, 2, 12, 20, 0), new Date(2014, 6, 2, 12, 7, 59) ) //=> 12
Example 2
// How many minutes are between 10:01:59 and 10:00:00 const result = differenceInMinutes( new Date(2000, 0, 1, 10, 0, 0), new Date(2000, 0, 1, 10, 1, 59) ) //=> -1
function differenceInMonths
differenceInMonths: ( laterDate: DateArg<Date> & {}, earlierDate: DateArg<Date> & {}, options?: DifferenceInMonthsOptions | undefined) => number;
differenceInMonths Month Helpers Get the number of full months between the given dates.
Parameter laterDate
The later date
Parameter earlierDate
The earlier date
Parameter options
An object with options
Returns
The number of full months
Example 1
// How many full months are between 31 January 2014 and 1 September 2014? const result = differenceInMonths(new Date(2014, 8, 1), new Date(2014, 0, 31)) //=> 7
function differenceInQuarters
differenceInQuarters: ( laterDate: DateArg<Date> & {}, earlierDate: DateArg<Date> & {}, options?: DifferenceInQuartersOptions | undefined) => number;
differenceInQuarters Quarter Helpers Get the number of quarters between the given dates.
Get the number of quarters between the given dates.
Parameter laterDate
The later date
Parameter earlierDate
The earlier date
Parameter options
An object with options.
Returns
The number of full quarters
Example 1
// How many full quarters are between 31 December 2013 and 2 July 2014? const result = differenceInQuarters(new Date(2014, 6, 2), new Date(2013, 11, 31)) //=> 2
function differenceInSeconds
differenceInSeconds: ( laterDate: DateArg<Date> & {}, earlierDate: DateArg<Date> & {}, options?: DifferenceInSecondsOptions) => number;
differenceInSeconds Second Helpers Get the number of seconds between the given dates.
Get the number of seconds between the given dates.
Parameter laterDate
The later date
Parameter earlierDate
The earlier date
Parameter options
An object with options.
Returns
The number of seconds
Example 1
// How many seconds are between // 2 July 2014 12:30:07.999 and 2 July 2014 12:30:20.000? const result = differenceInSeconds( new Date(2014, 6, 2, 12, 30, 20, 0), new Date(2014, 6, 2, 12, 30, 7, 999) ) //=> 12
function differenceInWeeks
differenceInWeeks: ( laterDate: DateArg<Date> & {}, earlierDate: DateArg<Date> & {}, options?: DifferenceInWeeksOptions | undefined) => number;
differenceInWeeks Week Helpers Get the number of full weeks between the given dates.
Get the number of full weeks between two dates. Fractional weeks are truncated towards zero by default.
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.trunc(differenceInHours(dateLeft, dateRight)/(7*24))|0
.Parameter laterDate
The later date
Parameter earlierDate
The earlier date
Parameter options
An object with options
Returns
The number of full weeks
Example 1
// How many full weeks are between 5 July 2014 and 20 July 2014? const result = differenceInWeeks(new Date(2014, 6, 20), new Date(2014, 6, 5)) //=> 2
Example 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. const result = differenceInWeeks( new Date(2020, 5, 1), new Date(2020, 2, 6) ) //=> 8
function differenceInYears
differenceInYears: ( laterDate: DateArg<Date> & {}, earlierDate: DateArg<Date> & {}, options?: DifferenceInYearsOptions | undefined) => number;
differenceInYears Year Helpers Get the number of full years between the given dates.
Get the number of full years between the given dates.
Parameter laterDate
The later date
Parameter earlierDate
The earlier date
Parameter options
An object with options
Returns
The number of full years
Example 1
// How many full years are between 31 December 2013 and 11 February 2015? const result = differenceInYears(new Date(2015, 1, 11), new Date(2013, 11, 31)) //=> 1
function eachDayOfInterval
eachDayOfInterval: < IntervalType extends Interval<DateArg<Date>, DateArg<Date>>, Options extends EachDayOfIntervalOptions<Date> = undefined>( interval: IntervalType, options?: Options) => EachDayOfIntervalResult<IntervalType, Options>;
eachDayOfInterval Interval Helpers Return the array of dates within the specified time interval.
Return the array of dates within the specified time interval.
Parameter interval
The interval.
Parameter options
An object with options.
Returns
The array with starts of days from the day of the interval start to the day of the interval end
Example 1
// Each day between 6 October 2014 and 10 October 2014: const result = eachDayOfInterval({ start: new Date(2014, 9, 6), end: new Date(2014, 9, 10) }) //=> [ // Mon Oct 06 2014 00:00:00, // Tue Oct 07 2014 00:00:00, // Wed Oct 08 2014 00:00:00, // Thu Oct 09 2014 00:00:00, // Fri Oct 10 2014 00:00:00 // ]
function eachHourOfInterval
eachHourOfInterval: < IntervalType extends Interval<DateArg<Date>, DateArg<Date>>, Options extends EachHourOfIntervalOptions<Date> = undefined>( interval: IntervalType, options?: Options) => EachHourOfIntervalResult<IntervalType, Options>;
eachHourOfInterval Interval Helpers Return the array of hours within the specified time interval.
Return the array of hours within the specified time interval.
Parameter interval
The interval.
Parameter options
An object with options.
Returns
The array with starts of hours from the hour of the interval start to the hour of the interval end
Example 1
// Each hour between 6 October 2014, 12:00 and 6 October 2014, 15:00 const result = eachHourOfInterval({ start: new Date(2014, 9, 6, 12), end: new Date(2014, 9, 6, 15) }); //=> [ // Mon Oct 06 2014 12:00:00, // Mon Oct 06 2014 13:00:00, // Mon Oct 06 2014 14:00:00, // Mon Oct 06 2014 15:00:00 // ]
function eachMinuteOfInterval
eachMinuteOfInterval: < IntervalType extends Interval<DateArg<Date>, DateArg<Date>>, Options extends EachMinuteOfIntervalOptions<Date> = undefined>( interval: IntervalType, options?: Options) => EachMinuteOfIntervalResult<IntervalType, Options>;
eachMinuteOfInterval Interval Helpers Return the array of minutes within the specified time interval.
Returns the array of minutes within the specified time interval.
Parameter interval
The interval.
Parameter options
An object with options.
Returns
The array with starts of minutes from the minute of the interval start to the minute of the interval end
Example 1
// Each minute between 14 October 2020, 13:00 and 14 October 2020, 13:03 const result = eachMinuteOfInterval({ start: new Date(2014, 9, 14, 13), end: new Date(2014, 9, 14, 13, 3) }) //=> [ // Wed Oct 14 2014 13:00:00, // Wed Oct 14 2014 13:01:00, // Wed Oct 14 2014 13:02:00, // Wed Oct 14 2014 13:03:00 // ]
function eachMonthOfInterval
eachMonthOfInterval: < IntervalType extends Interval<DateArg<Date>, DateArg<Date>>, Options extends EachMonthOfIntervalOptions<Date> = undefined>( interval: IntervalType, options?: Options) => EachMonthOfIntervalResult<IntervalType, Options>;
eachMonthOfInterval Interval Helpers Return the array of months within the specified time interval.
Return the array of months within the specified time interval.
Parameter interval
The interval.
Parameter options
An object with options.
Returns
The array with starts of months from the month of the interval start to the month of the interval end
Example 1
// Each month between 6 February 2014 and 10 August 2014: const result = eachMonthOfInterval({ start: new Date(2014, 1, 6), end: new Date(2014, 7, 10) }) //=> [ // Sat Feb 01 2014 00:00:00, // Sat Mar 01 2014 00:00:00, // Tue Apr 01 2014 00:00:00, // Thu May 01 2014 00:00:00, // Sun Jun 01 2014 00:00:00, // Tue Jul 01 2014 00:00:00, // Fri Aug 01 2014 00:00:00 // ]
function eachQuarterOfInterval
eachQuarterOfInterval: < IntervalType extends Interval<DateArg<Date>, DateArg<Date>>, Options extends EachQuarterOfIntervalOptions<Date> = undefined>( interval: IntervalType, options?: Options) => EachQuarterOfIntervalResult<IntervalType, Options>;
eachQuarterOfInterval Interval Helpers Return the array of quarters within the specified time interval.
Return the array of quarters within the specified time interval.
Parameter interval
The interval
Parameter options
An object with options
Returns
The array with starts of quarters from the quarter of the interval start to the quarter of the interval end
Example 1
// Each quarter within interval 6 February 2014 - 10 August 2014: const result = eachQuarterOfInterval({ start: new Date(2014, 1, 6), end: new Date(2014, 7, 10), }) //=> [ // Wed Jan 01 2014 00:00:00, // Tue Apr 01 2014 00:00:00, // Tue Jul 01 2014 00:00:00, // ]
function eachWeekendOfInterval
eachWeekendOfInterval: < IntervalType extends Interval<DateArg<Date>, DateArg<Date>>, Options extends EachWeekendOfIntervalOptions<Date> = undefined>( interval: IntervalType, options?: Options) => EachWeekendOfIntervalResult<IntervalType, Options>;
eachWeekendOfInterval Interval Helpers List all the Saturdays and Sundays in the given date interval.
Get all the Saturdays and Sundays in the given date interval.
Parameter interval
The given interval
Parameter options
An object with options
Returns
An array containing all the Saturdays and Sundays
Example 1
// Lists all Saturdays and Sundays in the given date interval const result = eachWeekendOfInterval({ start: new Date(2018, 8, 17), end: new Date(2018, 8, 30) }) //=> [ // Sat Sep 22 2018 00:00:00, // Sun Sep 23 2018 00:00:00, // Sat Sep 29 2018 00:00:00, // Sun Sep 30 2018 00:00:00 // ]
function eachWeekendOfMonth
eachWeekendOfMonth: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, options?: EachWeekendOfMonthOptions<ResultDate>) => ResultDate[];
eachWeekendOfMonth Month Helpers List all the Saturdays and Sundays in the given month.
Get all the Saturdays and Sundays in the given month.
Parameter date
The given month
Parameter options
An object with options
Returns
An array containing all the Saturdays and Sundays
Example 1
// Lists all Saturdays and Sundays in the given month const result = eachWeekendOfMonth(new Date(2022, 1, 1)) //=> [ // Sat Feb 05 2022 00:00:00, // Sun Feb 06 2022 00:00:00, // Sat Feb 12 2022 00:00:00, // Sun Feb 13 2022 00:00:00, // Sat Feb 19 2022 00:00:00, // Sun Feb 20 2022 00:00:00, // Sat Feb 26 2022 00:00:00, // Sun Feb 27 2022 00:00:00 // ]
function eachWeekendOfYear
eachWeekendOfYear: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, options?: EachWeekendOfYearOptions<ResultDate>) => ResultDate[];
eachWeekendOfYear Year Helpers List all the Saturdays and Sundays in the year.
Get all the Saturdays and Sundays in the year.
Parameter date
The given year
Parameter options
An object with options
Returns
An array containing all the Saturdays and Sundays
Example 1
// Lists all Saturdays and Sundays in the year const result = eachWeekendOfYear(new Date(2020, 1, 1)) //=> [ // Sat Jan 03 2020 00:00:00, // Sun Jan 04 2020 00:00:00, // ... // Sun Dec 27 2020 00:00:00 // ] ]
function eachWeekOfInterval
eachWeekOfInterval: < IntervalType extends Interval<DateArg<Date>, DateArg<Date>>, Options extends EachWeekOfIntervalOptions<Date> = undefined>( interval: IntervalType, options?: Options) => EachWeekOfIntervalResult<IntervalType, Options>;
eachWeekOfInterval Interval Helpers Return the array of weeks within the specified time interval.
Return the array of weeks within the specified time interval.
Parameter interval
The interval.
Parameter options
An object with options.
Returns
The array with starts of weeks from the week of the interval start to the week of the interval end
Example 1
// Each week within interval 6 October 2014 - 23 November 2014: const result = eachWeekOfInterval({ start: new Date(2014, 9, 6), end: new Date(2014, 10, 23) }) //=> [ // Sun Oct 05 2014 00:00:00, // Sun Oct 12 2014 00:00:00, // Sun Oct 19 2014 00:00:00, // Sun Oct 26 2014 00:00:00, // Sun Nov 02 2014 00:00:00, // Sun Nov 09 2014 00:00:00, // Sun Nov 16 2014 00:00:00, // Sun Nov 23 2014 00:00:00 // ]
function eachYearOfInterval
eachYearOfInterval: < IntervalType extends Interval<DateArg<Date>, DateArg<Date>>, Options extends EachYearOfIntervalOptions<Date> = undefined>( interval: IntervalType, options?: Options) => EachYearOfIntervalResult<IntervalType, Options>;
eachYearOfInterval Interval Helpers Return the array of yearly timestamps within the specified time interval.
Return the array of yearly timestamps within the specified time interval.
Parameter interval
The interval.
Parameter options
An object with options.
Returns
The array with starts of yearly timestamps from the month of the interval start to the month of the interval end
Example 1
// Each year between 6 February 2014 and 10 August 2017: const result = eachYearOfInterval({ start: new Date(2014, 1, 6), end: new Date(2017, 7, 10) }) //=> [ // Wed Jan 01 2014 00:00:00, // Thu Jan 01 2015 00:00:00, // Fri Jan 01 2016 00:00:00, // Sun Jan 01 2017 00:00:00 // ]
function endOfDay
endOfDay: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, options?: EndOfDayOptions<ResultDate> | undefined) => ResultDate;
endOfDay Day Helpers Return the end of a day for the given date.
Return the end of a day for the given date. The result will be in the local timezone.
Parameter date
The original date
Parameter options
An object with options
Returns
The end of a day
Example 1
// 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 endOfDecade
endOfDecade: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, options?: EndOfDecadeOptions<ResultDate> | undefined) => ResultDate;
endOfDecade Decade Helpers Return the end of a decade for the given date.
Return the end of a decade for the given date.
Parameter date
The original date
Parameter options
An object with options
Returns
The end of a decade
Example 1
// The end of a decade for 12 May 1984 00:00:00: const result = endOfDecade(new Date(1984, 4, 12, 00, 00, 00)) //=> Dec 31 1989 23:59:59.999
function endOfHour
endOfHour: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, options?: EndOfHourOptions<ResultDate> | undefined) => ResultDate;
endOfHour Hour Helpers Return the end of an hour for the given date.
Return the end of an hour for the given date. The result will be in the local timezone.
Parameter date
The original date
Parameter options
An object with options
Returns
The end of an hour
Example 1
// 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 endOfISOWeek
endOfISOWeek: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, options?: EndOfISOWeekOptions<ResultDate> | undefined) => ResultDate;
endOfISOWeek ISO Week Helpers Return the end of an ISO week for the given date.
Return the end of an ISO week for the given date. The result will be in the local timezone.
ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_date
Parameter date
The original date
Parameter options
An object with options
Returns
The end of an ISO week
Example 1
// The end of an ISO week for 2 September 2014 11:55:00: const result = endOfISOWeek(new Date(2014, 8, 2, 11, 55, 0)) //=> Sun Sep 07 2014 23:59:59.999
function endOfISOWeekYear
endOfISOWeekYear: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, options?: EndOfISOWeekYearOptions<ResultDate> | undefined) => ResultDate;
endOfISOWeekYear ISO Week-Numbering Year Helpers Return the end of an ISO week-numbering year for the given date.
Return the end of an ISO week-numbering year, which always starts 3 days before the year's first Thursday. The result will be in the local timezone.
ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_date
Parameter date
The original date
Parameter options
The options
Returns
The end of an ISO week-numbering year
Example 1
// The end of an ISO week-numbering year for 2 July 2005: const result = endOfISOWeekYear(new Date(2005, 6, 2)) //=> Sun Jan 01 2006 23:59:59.999
function endOfMinute
endOfMinute: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, options?: EndOfMinuteOptions<ResultDate> | undefined) => ResultDate;
endOfMinute Minute Helpers Return the end of a minute for the given date.
Return the end of a minute for the given date. The result will be in the local timezone or the provided context.
Parameter date
The original date
Parameter options
An object with options
Returns
The end of a minute
Example 1
// 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 endOfMonth
endOfMonth: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, options?: EndOfMonthOptions<ResultDate> | undefined) => ResultDate;
endOfMonth Month Helpers Return the end of a month for the given date.
Return the end of a month for the given date. The result will be in the local timezone.
Parameter date
The original date
Parameter options
An object with options
Returns
The end of a month
Example 1
// 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 endOfQuarter
endOfQuarter: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, options?: EndOfQuarterOptions<ResultDate> | undefined) => ResultDate;
endOfQuarter Quarter Helpers Return the end of a year quarter for the given date.
Return the end of a year quarter for the given date. The result will be in the local timezone.
Parameter date
The original date
Parameter options
An object with options
Returns
The end of a quarter
Example 1
// The end of a quarter for 2 September 2014 11:55:00: const result = endOfQuarter(new Date(2014, 8, 2, 11, 55, 0)) //=> Tue Sep 30 2014 23:59:59.999
function endOfSecond
endOfSecond: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, options?: EndOfSecondOptions<ResultDate> | undefined) => ResultDate;
endOfSecond Second Helpers Return the end of a second for the given date.
Return the end of a second for the given date. The result will be in the local timezone if no
in
option is specified.Parameter date
The original date
Parameter options
An object with options
Returns
The end of a second
Example 1
// The end of a second for 1 December 2014 22:15:45.400: const result = endOfSecond(new Date(2014, 11, 1, 22, 15, 45, 400)) //=> Mon Dec 01 2014 22:15:45.999
function endOfToday
endOfToday: <ResultDate extends Date = Date>( options?: EndOfTodayOptions<ResultDate>) => ResultDate;
endOfToday Day Helpers Return the end of today. false
Return the end of today.
Parameter options
The options
Returns
The end of today
Example 1
// If today is 6 October 2014: const result = endOfToday() //=> Mon Oct 6 2014 23:59:59.999
function endOfTomorrow
endOfTomorrow: <DateType extends Date, ResultDate extends Date = DateType>( options?: EndOfTomorrowOptions<ResultDate> | undefined) => ResultDate;
endOfTomorrow Day Helpers Return the end of tomorrow. false
Return the end of tomorrow.
Parameter options
The options
Returns
The end of tomorrow
Example 1
// If today is 6 October 2014: const result = endOfTomorrow() //=> Tue Oct 7 2014 23:59:59.999
function endOfWeek
endOfWeek: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, options?: EndOfWeekOptions<ResultDate>) => ResultDate;
endOfWeek Week Helpers Return the end of a week for the given date.
Return the end of a week for the given date. The result will be in the local timezone.
Parameter date
The original date
Parameter options
An object with options
Returns
The end of a week
Example 1
// 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 2
// 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 endOfYear
endOfYear: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, options?: EndOfYearOptions<ResultDate>) => ResultDate;
endOfYear Year Helpers Return the end of a year for the given date.
Return the end of a year for the given date. The result will be in the local timezone.
Parameter date
The original date
Parameter options
The options
Returns
The end of a year
Example 1
// The end of a year for 2 September 2014 11:55:00: const result = endOfYear(new Date(2014, 8, 2, 11, 55, 0)) //=> Wed Dec 31 2014 23:59:59.999
function endOfYesterday
endOfYesterday: <DateType extends Date, ResultDate extends Date = DateType>( options?: EndOfYesterdayOptions<ResultDate> | undefined) => ResultDate;
endOfYesterday Day Helpers Return the end of yesterday. false
Return the end of yesterday.
Returns
The end of yesterday
Example 1
// If today is 6 October 2014: const result = endOfYesterday() //=> Sun Oct 5 2014 23:59:59.999
function format
format: ( date: DateArg<Date> & {}, formatStr: string, options?: FormatOptions) => string;
format formatDate Common Helpers Format the date.
Return the formatted date string in the given format. The result may vary by locale.
> ⚠️ Please note that the
format
tokens differ from Moment.js and other libraries. > See: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.mdThe characters wrapped between two single quotes characters (') are escaped. Two single quotes in a row, whether inside or outside a quoted sequence, represent a 'real' single quote. (see the last example)
Format of the string is based on Unicode Technical Standard #35: https://www.unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table with a few additions (see note 7 below the table).
Accepted patterns: | Unit | Pattern | Result examples | Notes | |---------------------------------|---------|-----------------------------------|-------| | Era | G..GGG | AD, BC | | | | GGGG | Anno Domini, Before Christ | 2 | | | GGGGG | A, B | | | Calendar year | y | 44, 1, 1900, 2017 | 5 | | | yo | 44th, 1st, 0th, 17th | 5,7 | | | yy | 44, 01, 00, 17 | 5 | | | yyy | 044, 001, 1900, 2017 | 5 | | | yyyy | 0044, 0001, 1900, 2017 | 5 | | | yyyyy | ... | 3,5 | | Local week-numbering year | Y | 44, 1, 1900, 2017 | 5 | | | Yo | 44th, 1st, 1900th, 2017th | 5,7 | | | YY | 44, 01, 00, 17 | 5,8 | | | YYY | 044, 001, 1900, 2017 | 5 | | | YYYY | 0044, 0001, 1900, 2017 | 5,8 | | | YYYYY | ... | 3,5 | | ISO week-numbering year | R | -43, 0, 1, 1900, 2017 | 5,7 | | | RR | -43, 00, 01, 1900, 2017 | 5,7 | | | RRR | -043, 000, 001, 1900, 2017 | 5,7 | | | RRRR | -0043, 0000, 0001, 1900, 2017 | 5,7 | | | RRRRR | ... | 3,5,7 | | Extended year | u | -43, 0, 1, 1900, 2017 | 5 | | | uu | -43, 01, 1900, 2017 | 5 | | | uuu | -043, 001, 1900, 2017 | 5 | | | uuuu | -0043, 0001, 1900, 2017 | 5 | | | uuuuu | ... | 3,5 | | Quarter (formatting) | Q | 1, 2, 3, 4 | | | | Qo | 1st, 2nd, 3rd, 4th | 7 | | | QQ | 01, 02, 03, 04 | | | | QQQ | Q1, Q2, Q3, Q4 | | | | QQQQ | 1st quarter, 2nd quarter, ... | 2 | | | QQQQQ | 1, 2, 3, 4 | 4 | | Quarter (stand-alone) | q | 1, 2, 3, 4 | | | | qo | 1st, 2nd, 3rd, 4th | 7 | | | qq | 01, 02, 03, 04 | | | | qqq | Q1, Q2, Q3, Q4 | | | | qqqq | 1st quarter, 2nd quarter, ... | 2 | | | qqqqq | 1, 2, 3, 4 | 4 | | Month (formatting) | M | 1, 2, ..., 12 | | | | Mo | 1st, 2nd, ..., 12th | 7 | | | MM | 01, 02, ..., 12 | | | | MMM | Jan, Feb, ..., Dec | | | | MMMM | January, February, ..., December | 2 | | | MMMMM | J, F, ..., D | | | Month (stand-alone) | L | 1, 2, ..., 12 | | | | Lo | 1st, 2nd, ..., 12th | 7 | | | LL | 01, 02, ..., 12 | | | | LLL | Jan, Feb, ..., Dec | | | | LLLL | January, February, ..., December | 2 | | | LLLLL | J, F, ..., D | | | Local week of year | w | 1, 2, ..., 53 | | | | wo | 1st, 2nd, ..., 53th | 7 | | | ww | 01, 02, ..., 53 | | | ISO week of year | I | 1, 2, ..., 53 | 7 | | | Io | 1st, 2nd, ..., 53th | 7 | | | II | 01, 02, ..., 53 | 7 | | Day of month | d | 1, 2, ..., 31 | | | | do | 1st, 2nd, ..., 31st | 7 | | | dd | 01, 02, ..., 31 | | | Day of year | D | 1, 2, ..., 365, 366 | 9 | | | Do | 1st, 2nd, ..., 365th, 366th | 7 | | | DD | 01, 02, ..., 365, 366 | 9 | | | DDD | 001, 002, ..., 365, 366 | | | | DDDD | ... | 3 | | Day of week (formatting) | E..EEE | Mon, Tue, Wed, ..., Sun | | | | EEEE | Monday, Tuesday, ..., Sunday | 2 | | | EEEEE | M, T, W, T, F, S, S | | | | EEEEEE | Mo, Tu, We, Th, Fr, Sa, Su | | | ISO day of week (formatting) | i | 1, 2, 3, ..., 7 | 7 | | | io | 1st, 2nd, ..., 7th | 7 | | | ii | 01, 02, ..., 07 | 7 | | | iii | Mon, Tue, Wed, ..., Sun | 7 | | | iiii | Monday, Tuesday, ..., Sunday | 2,7 | | | iiiii | M, T, W, T, F, S, S | 7 | | | iiiiii | Mo, Tu, We, Th, Fr, Sa, Su | 7 | | Local day of week (formatting) | e | 2, 3, 4, ..., 1 | | | | eo | 2nd, 3rd, ..., 1st | 7 | | | ee | 02, 03, ..., 01 | | | | eee | Mon, Tue, Wed, ..., Sun | | | | eeee | Monday, Tuesday, ..., Sunday | 2 | | | eeeee | M, T, W, T, F, S, S | | | | eeeeee | Mo, Tu, We, Th, Fr, Sa, Su | | | Local day of week (stand-alone) | c | 2, 3, 4, ..., 1 | | | | co | 2nd, 3rd, ..., 1st | 7 | | | cc | 02, 03, ..., 01 | | | | ccc | Mon, Tue, Wed, ..., Sun | | | | cccc | Monday, Tuesday, ..., Sunday | 2 | | | ccccc | M, T, W, T, F, S, S | | | | cccccc | Mo, Tu, We, Th, Fr, Sa, Su | | | AM, PM | a..aa | AM, PM | | | | aaa | am, pm | | | | aaaa | a.m., p.m. | 2 | | | aaaaa | a, p | | | AM, PM, noon, midnight | b..bb | AM, PM, noon, midnight | | | | bbb | am, pm, noon, midnight | | | | bbbb | a.m., p.m., noon, midnight | 2 | | | bbbbb | a, p, n, mi | | | Flexible day period | B..BBB | at night, in the morning, ... | | | | BBBB | at night, in the morning, ... | 2 | | | BBBBB | at night, in the morning, ... | | | Hour [1-12] | h | 1, 2, ..., 11, 12 | | | | ho | 1st, 2nd, ..., 11th, 12th | 7 | | | hh | 01, 02, ..., 11, 12 | | | Hour [0-23] | H | 0, 1, 2, ..., 23 | | | | Ho | 0th, 1st, 2nd, ..., 23rd | 7 | | | HH | 00, 01, 02, ..., 23 | | | Hour [0-11] | K | 1, 2, ..., 11, 0 | | | | Ko | 1st, 2nd, ..., 11th, 0th | 7 | | | KK | 01, 02, ..., 11, 00 | | | Hour [1-24] | k | 24, 1, 2, ..., 23 | | | | ko | 24th, 1st, 2nd, ..., 23rd | 7 | | | kk | 24, 01, 02, ..., 23 | | | Minute | m | 0, 1, ..., 59 | | | | mo | 0th, 1st, ..., 59th | 7 | | | mm | 00, 01, ..., 59 | | | Second | s | 0, 1, ..., 59 | | | | so | 0th, 1st, ..., 59th | 7 | | | ss | 00, 01, ..., 59 | | | Fraction of second | S | 0, 1, ..., 9 | | | | SS | 00, 01, ..., 99 | | | | SSS | 000, 001, ..., 999 | | | | SSSS | ... | 3 | | Timezone (ISO-8601 w/ Z) | X | -08, +0530, Z | | | | XX | -0800, +0530, Z | | | | XXX | -08:00, +05:30, Z | | | | XXXX | -0800, +0530, Z, +123456 | 2 | | | XXXXX | -08:00, +05:30, Z, +12:34:56 | | | Timezone (ISO-8601 w/o Z) | x | -08, +0530, +00 | | | | xx | -0800, +0530, +0000 | | | | xxx | -08:00, +05:30, +00:00 | 2 | | | xxxx | -0800, +0530, +0000, +123456 | | | | xxxxx | -08:00, +05:30, +00:00, +12:34:56 | | | Timezone (GMT) | O...OOO | GMT-8, GMT+5:30, GMT+0 | | | | OOOO | GMT-08:00, GMT+05:30, GMT+00:00 | 2 | | Timezone (specific non-locat.) | z...zzz | GMT-8, GMT+5:30, GMT+0 | 6 | | | zzzz | GMT-08:00, GMT+05:30, GMT+00:00 | 2,6 | | Seconds timestamp | t | 512969520 | 7 | | | tt | ... | 3,7 | | Milliseconds timestamp | T | 512969520900 | 7 | | | TT | ... | 3,7 | | Long localized date | P | 04/29/1453 | 7 | | | PP | Apr 29, 1453 | 7 | | | PPP | April 29th, 1453 | 7 | | | PPPP | Friday, April 29th, 1453 | 2,7 | | Long localized time | p | 12:00 AM | 7 | | | pp | 12:00:00 AM | 7 | | | ppp | 12:00:00 AM GMT+2 | 7 | | | pppp | 12:00:00 AM GMT+02:00 | 2,7 | | Combination of date and time | Pp | 04/29/1453, 12:00 AM | 7 | | | PPpp | Apr 29, 1453, 12:00:00 AM | 7 | | | PPPppp | April 29th, 1453 at ... | 7 | | | PPPPpppp| Friday, April 29th, 1453 at ... | 2,7 | Notes: 1. "Formatting" units (e.g. formatting quarter) in the default en-US locale are the same as "stand-alone" units, but are different in some languages. "Formatting" units are declined according to the rules of the language in the context of a date. "Stand-alone" units are always nominative singular:
format(new Date(2017, 10, 6), 'do LLLL', {locale: cs}) //=> '6. listopad'
format(new Date(2017, 10, 6), 'do MMMM', {locale: cs}) //=> '6. listopadu'
2. Any sequence of the identical letters is a pattern, unless it is escaped by the single quote characters (see below). If the sequence is longer than listed in table (e.g.
EEEEEEEEEEE
) the output will be the same as default pattern for this unit, usually the longest one (in case of ISO weekdays,EEEE
). Default patterns for units are marked with "2" in the last column of the table.format(new Date(2017, 10, 6), 'MMM') //=> 'Nov'
format(new Date(2017, 10, 6), 'MMMM') //=> 'November'
format(new Date(2017, 10, 6), 'MMMMM') //=> 'N'
format(new Date(2017, 10, 6), 'MMMMMM') //=> 'November'
format(new Date(2017, 10, 6), 'MMMMMMM') //=> 'November'
3. Some patterns could be unlimited length (such as
yyyyyyyy
). The output will be padded with zeros to match the length of the pattern.format(new Date(2017, 10, 6), 'yyyyyyyy') //=> '00002017'
4.
QQQQQ
andqqqqq
could be not strictly numerical in some locales. These tokens represent the shortest form of the quarter.5. The main difference between
y
andu
patterns are B.C. years:| Year |
y
|u
| |------|-----|-----| | AC 1 | 1 | 1 | | BC 1 | 1 | 0 | | BC 2 | 2 | -1 |Also
yy
always returns the last two digits of a year, whileuu
pads single digit years to 2 characters and returns other years unchanged:| Year |
yy
|uu
| |------|------|------| | 1 | 01 | 01 | | 14 | 14 | 14 | | 376 | 76 | 376 | | 1453 | 53 | 1453 |The same difference is true for local and ISO week-numbering years (
Y
andR
), except local week-numbering years are dependent onoptions.weekStartsOn
andoptions.firstWeekContainsDate
(compare [getISOWeekYear](https://date-fns.org/docs/getISOWeekYear) and [getWeekYear](https://date-fns.org/docs/getWeekYear)).6. Specific non-location timezones are currently unavailable in
date-fns
, so right now these tokens fall back to GMT timezones.7. These patterns are not in the Unicode Technical Standard #35: -
i
: ISO day of week -I
: ISO week of year -R
: ISO week-numbering year -t
: seconds timestamp -T
: milliseconds timestamp -o
: ordinal number modifier -P
: long localized date -p
: long localized time8.
YY
andYYYY
tokens represent week-numbering years but they are often confused with years. You should enableoptions.useAdditionalWeekYearTokens
to use them. See: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md9.
D
andDD
tokens represent days of the year but they are often confused with days of the month. You should enableoptions.useAdditionalDayOfYearTokens
to use them. See: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.mdParameter date
The original date
Parameter format
The string of tokens
Parameter options
An object with options
Returns
The formatted date string
Throws
date
must not be Invalid DateThrows
options.locale
must containlocalize
propertyThrows
options.locale
must containformatLong
propertyThrows
use
yyyy
instead ofYYYY
for formatting years using [format provided] to the input [input provided]; see: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.mdThrows
use
yy
instead ofYY
for formatting years using [format provided] to the input [input provided]; see: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.mdThrows
use
d
instead ofD
for formatting days of the month using [format provided] to the input [input provided]; see: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.mdThrows
use
dd
instead ofDD
for formatting days of the month using [format provided] to the input [input provided]; see: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.mdThrows
format string contains an unescaped latin alphabet character
Example 1
// Represent 11 February 2014 in middle-endian format: const result = format(new Date(2014, 1, 11), 'MM/dd/yyyy') //=> '02/11/2014'
Example 2
// Represent 2 July 2014 in Esperanto: import { eoLocale } from 'date-fns/locale/eo' const result = format(new Date(2014, 6, 2), "do 'de' MMMM yyyy", { locale: eoLocale }) //=> '2-a de julio 2014'
Example 3
// Escape string by single quote characters: const result = format(new Date(2014, 6, 2, 15), "h 'o''clock'") //=> "3 o'clock"
function formatDate
formatDate: ( date: DateArg<Date> & {}, formatStr: string, options?: FormatOptions) => string;
format formatDate Common Helpers Format the date.
Return the formatted date string in the given format. The result may vary by locale.
> ⚠️ Please note that the
format
tokens differ from Moment.js and other libraries. > See: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.mdThe characters wrapped between two single quotes characters (') are escaped. Two single quotes in a row, whether inside or outside a quoted sequence, represent a 'real' single quote. (see the last example)
Format of the string is based on Unicode Technical Standard #35: https://www.unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table with a few additions (see note 7 below the table).
Accepted patterns: | Unit | Pattern | Result examples | Notes | |---------------------------------|---------|-----------------------------------|-------| | Era | G..GGG | AD, BC | | | | GGGG | Anno Domini, Before Christ | 2 | | | GGGGG | A, B | | | Calendar year | y | 44, 1, 1900, 2017 | 5 | | | yo | 44th, 1st, 0th, 17th | 5,7 | | | yy | 44, 01, 00, 17 | 5 | | | yyy | 044, 001, 1900, 2017 | 5 | | | yyyy | 0044, 0001, 1900, 2017 | 5 | | | yyyyy | ... | 3,5 | | Local week-numbering year | Y | 44, 1, 1900, 2017 | 5 | | | Yo | 44th, 1st, 1900th, 2017th | 5,7 | | | YY | 44, 01, 00, 17 | 5,8 | | | YYY | 044, 001, 1900, 2017 | 5 | | | YYYY | 0044, 0001, 1900, 2017 | 5,8 | | | YYYYY | ... | 3,5 | | ISO week-numbering year | R | -43, 0, 1, 1900, 2017 | 5,7 | | | RR | -43, 00, 01, 1900, 2017 | 5,7 | | | RRR | -043, 000, 001, 1900, 2017 | 5,7 | | | RRRR | -0043, 0000, 0001, 1900, 2017 | 5,7 | | | RRRRR | ... | 3,5,7 | | Extended year | u | -43, 0, 1, 1900, 2017 | 5 | | | uu | -43, 01, 1900, 2017 | 5 | | | uuu | -043, 001, 1900, 2017 | 5 | | | uuuu | -0043, 0001, 1900, 2017 | 5 | | | uuuuu | ... | 3,5 | | Quarter (formatting) | Q | 1, 2, 3, 4 | | | | Qo | 1st, 2nd, 3rd, 4th | 7 | | | QQ | 01, 02, 03, 04 | | | | QQQ | Q1, Q2, Q3, Q4 | | | | QQQQ | 1st quarter, 2nd quarter, ... | 2 | | | QQQQQ | 1, 2, 3, 4 | 4 | | Quarter (stand-alone) | q | 1, 2, 3, 4 | | | | qo | 1st, 2nd, 3rd, 4th | 7 | | | qq | 01, 02, 03, 04 | | | | qqq | Q1, Q2, Q3, Q4 | | | | qqqq | 1st quarter, 2nd quarter, ... | 2 | | | qqqqq | 1, 2, 3, 4 | 4 | | Month (formatting) | M | 1, 2, ..., 12 | | | | Mo | 1st, 2nd, ..., 12th | 7 | | | MM | 01, 02, ..., 12 | | | | MMM | Jan, Feb, ..., Dec | | | | MMMM | January, February, ..., December | 2 | | | MMMMM | J, F, ..., D | | | Month (stand-alone) | L | 1, 2, ..., 12 | | | | Lo | 1st, 2nd, ..., 12th | 7 | | | LL | 01, 02, ..., 12 | | | | LLL | Jan, Feb, ..., Dec | | | | LLLL | January, February, ..., December | 2 | | | LLLLL | J, F, ..., D | | | Local week of year | w | 1, 2, ..., 53 | | | | wo | 1st, 2nd, ..., 53th | 7 | | | ww | 01, 02, ..., 53 | | | ISO week of year | I | 1, 2, ..., 53 | 7 | | | Io | 1st, 2nd, ..., 53th | 7 | | | II | 01, 02, ..., 53 | 7 | | Day of month | d | 1, 2, ..., 31 | | | | do | 1st, 2nd, ..., 31st | 7 | | | dd | 01, 02, ..., 31 | | | Day of year | D | 1, 2, ..., 365, 366 | 9 | | | Do | 1st, 2nd, ..., 365th, 366th | 7 | | | DD | 01, 02, ..., 365, 366 | 9 | | | DDD | 001, 002, ..., 365, 366 | | | | DDDD | ... | 3 | | Day of week (formatting) | E..EEE | Mon, Tue, Wed, ..., Sun | | | | EEEE | Monday, Tuesday, ..., Sunday | 2 | | | EEEEE | M, T, W, T, F, S, S | | | | EEEEEE | Mo, Tu, We, Th, Fr, Sa, Su | | | ISO day of week (formatting) | i | 1, 2, 3, ..., 7 | 7 | | | io | 1st, 2nd, ..., 7th | 7 | | | ii | 01, 02, ..., 07 | 7 | | | iii | Mon, Tue, Wed, ..., Sun | 7 | | | iiii | Monday, Tuesday, ..., Sunday | 2,7 | | | iiiii | M, T, W, T, F, S, S | 7 | | | iiiiii | Mo, Tu, We, Th, Fr, Sa, Su | 7 | | Local day of week (formatting) | e | 2, 3, 4, ..., 1 | | | | eo | 2nd, 3rd, ..., 1st | 7 | | | ee | 02, 03, ..., 01 | | | | eee | Mon, Tue, Wed, ..., Sun | | | | eeee | Monday, Tuesday, ..., Sunday | 2 | | | eeeee | M, T, W, T, F, S, S | | | | eeeeee | Mo, Tu, We, Th, Fr, Sa, Su | | | Local day of week (stand-alone) | c | 2, 3, 4, ..., 1 | | | | co | 2nd, 3rd, ..., 1st | 7 | | | cc | 02, 03, ..., 01 | | | | ccc | Mon, Tue, Wed, ..., Sun | | | | cccc | Monday, Tuesday, ..., Sunday | 2 | | | ccccc | M, T, W, T, F, S, S | | | | cccccc | Mo, Tu, We, Th, Fr, Sa, Su | | | AM, PM | a..aa | AM, PM | | | | aaa | am, pm | | | | aaaa | a.m., p.m. | 2 | | | aaaaa | a, p | | | AM, PM, noon, midnight | b..bb | AM, PM, noon, midnight | | | | bbb | am, pm, noon, midnight | | | | bbbb | a.m., p.m., noon, midnight | 2 | | | bbbbb | a, p, n, mi | | | Flexible day period | B..BBB | at night, in the morning, ... | | | | BBBB | at night, in the morning, ... | 2 | | | BBBBB | at night, in the morning, ... | | | Hour [1-12] | h | 1, 2, ..., 11, 12 | | | | ho | 1st, 2nd, ..., 11th, 12th | 7 | | | hh | 01, 02, ..., 11, 12 | | | Hour [0-23] | H | 0, 1, 2, ..., 23 | | | | Ho | 0th, 1st, 2nd, ..., 23rd | 7 | | | HH | 00, 01, 02, ..., 23 | | | Hour [0-11] | K | 1, 2, ..., 11, 0 | | | | Ko | 1st, 2nd, ..., 11th, 0th | 7 | | | KK | 01, 02, ..., 11, 00 | | | Hour [1-24] | k | 24, 1, 2, ..., 23 | | | | ko | 24th, 1st, 2nd, ..., 23rd | 7 | | | kk | 24, 01, 02, ..., 23 | | | Minute | m | 0, 1, ..., 59 | | | | mo | 0th, 1st, ..., 59th | 7 | | | mm | 00, 01, ..., 59 | | | Second | s | 0, 1, ..., 59 | | | | so | 0th, 1st, ..., 59th | 7 | | | ss | 00, 01, ..., 59 | | | Fraction of second | S | 0, 1, ..., 9 | | | | SS | 00, 01, ..., 99 | | | | SSS | 000, 001, ..., 999 | | | | SSSS | ... | 3 | | Timezone (ISO-8601 w/ Z) | X | -08, +0530, Z | | | | XX | -0800, +0530, Z | | | | XXX | -08:00, +05:30, Z | | | | XXXX | -0800, +0530, Z, +123456 | 2 | | | XXXXX | -08:00, +05:30, Z, +12:34:56 | | | Timezone (ISO-8601 w/o Z) | x | -08, +0530, +00 | | | | xx | -0800, +0530, +0000 | | | | xxx | -08:00, +05:30, +00:00 | 2 | | | xxxx | -0800, +0530, +0000, +123456 | | | | xxxxx | -08:00, +05:30, +00:00, +12:34:56 | | | Timezone (GMT) | O...OOO | GMT-8, GMT+5:30, GMT+0 | | | | OOOO | GMT-08:00, GMT+05:30, GMT+00:00 | 2 | | Timezone (specific non-locat.) | z...zzz | GMT-8, GMT+5:30, GMT+0 | 6 | | | zzzz | GMT-08:00, GMT+05:30, GMT+00:00 | 2,6 | | Seconds timestamp | t | 512969520 | 7 | | | tt | ... | 3,7 | | Milliseconds timestamp | T | 512969520900 | 7 | | | TT | ... | 3,7 | | Long localized date | P | 04/29/1453 | 7 | | | PP | Apr 29, 1453 | 7 | | | PPP | April 29th, 1453 | 7 | | | PPPP | Friday, April 29th, 1453 | 2,7 | | Long localized time | p | 12:00 AM | 7 | | | pp | 12:00:00 AM | 7 | | | ppp | 12:00:00 AM GMT+2 | 7 | | | pppp | 12:00:00 AM GMT+02:00 | 2,7 | | Combination of date and time | Pp | 04/29/1453, 12:00 AM | 7 | | | PPpp | Apr 29, 1453, 12:00:00 AM | 7 | | | PPPppp | April 29th, 1453 at ... | 7 | | | PPPPpppp| Friday, April 29th, 1453 at ... | 2,7 | Notes: 1. "Formatting" units (e.g. formatting quarter) in the default en-US locale are the same as "stand-alone" units, but are different in some languages. "Formatting" units are declined according to the rules of the language in the context of a date. "Stand-alone" units are always nominative singular:
format(new Date(2017, 10, 6), 'do LLLL', {locale: cs}) //=> '6. listopad'
format(new Date(2017, 10, 6), 'do MMMM', {locale: cs}) //=> '6. listopadu'
2. Any sequence of the identical letters is a pattern, unless it is escaped by the single quote characters (see below). If the sequence is longer than listed in table (e.g.
EEEEEEEEEEE
) the output will be the same as default pattern for this unit, usually the longest one (in case of ISO weekdays,EEEE
). Default patterns for units are marked with "2" in the last column of the table.format(new Date(2017, 10, 6), 'MMM') //=> 'Nov'
format(new Date(2017, 10, 6), 'MMMM') //=> 'November'
format(new Date(2017, 10, 6), 'MMMMM') //=> 'N'
format(new Date(2017, 10, 6), 'MMMMMM') //=> 'November'
format(new Date(2017, 10, 6), 'MMMMMMM') //=> 'November'
3. Some patterns could be unlimited length (such as
yyyyyyyy
). The output will be padded with zeros to match the length of the pattern.format(new Date(2017, 10, 6), 'yyyyyyyy') //=> '00002017'
4.
QQQQQ
andqqqqq
could be not strictly numerical in some locales. These tokens represent the shortest form of the quarter.5. The main difference between
y
andu
patterns are B.C. years:| Year |
y
|u
| |------|-----|-----| | AC 1 | 1 | 1 | | BC 1 | 1 | 0 | | BC 2 | 2 | -1 |Also
yy
always returns the last two digits of a year, whileuu
pads single digit years to 2 characters and returns other years unchanged:| Year |
yy
|uu
| |------|------|------| | 1 | 01 | 01 | | 14 | 14 | 14 | | 376 | 76 | 376 | | 1453 | 53 | 1453 |The same difference is true for local and ISO week-numbering years (
Y
andR
), except local week-numbering years are dependent onoptions.weekStartsOn
andoptions.firstWeekContainsDate
(compare [getISOWeekYear](https://date-fns.org/docs/getISOWeekYear) and [getWeekYear](https://date-fns.org/docs/getWeekYear)).6. Specific non-location timezones are currently unavailable in
date-fns
, so right now these tokens fall back to GMT timezones.7. These patterns are not in the Unicode Technical Standard #35: -
i
: ISO day of week -I
: ISO week of year -R
: ISO week-numbering year -t
: seconds timestamp -T
: milliseconds timestamp -o
: ordinal number modifier -P
: long localized date -p
: long localized time8.
YY
andYYYY
tokens represent week-numbering years but they are often confused with years. You should enableoptions.useAdditionalWeekYearTokens
to use them. See: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md9.
D
andDD
tokens represent days of the year but they are often confused with days of the month. You should enableoptions.useAdditionalDayOfYearTokens
to use them. See: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.mdParameter date
The original date
Parameter format
The string of tokens
Parameter options
An object with options
Returns
The formatted date string
Throws
date
must not be Invalid DateThrows
options.locale
must containlocalize
propertyThrows
options.locale
must containformatLong
propertyThrows
use
yyyy
instead ofYYYY
for formatting years using [format provided] to the input [input provided]; see: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.mdThrows
use
yy
instead ofYY
for formatting years using [format provided] to the input [input provided]; see: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.mdThrows
use
d
instead ofD
for formatting days of the month using [format provided] to the input [input provided]; see: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.mdThrows
use
dd
instead ofDD
for formatting days of the month using [format provided] to the input [input provided]; see: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.mdThrows
format string contains an unescaped latin alphabet character
Example 1
// Represent 11 February 2014 in middle-endian format: const result = format(new Date(2014, 1, 11), 'MM/dd/yyyy') //=> '02/11/2014'
Example 2
// Represent 2 July 2014 in Esperanto: import { eoLocale } from 'date-fns/locale/eo' const result = format(new Date(2014, 6, 2), "do 'de' MMMM yyyy", { locale: eoLocale }) //=> '2-a de julio 2014'
Example 3
// Escape string by single quote characters: const result = format(new Date(2014, 6, 2, 15), "h 'o''clock'") //=> "3 o'clock"
function formatDistance
formatDistance: ( laterDate: DateArg<Date> & {}, earlierDate: DateArg<Date> & {}, options?: FormatDistanceOptions) => string;
formatDistance Common Helpers Return the distance between the given dates in words.
Return the distance between the given dates in words.
| Distance between dates | Result | |-------------------------------------------------------------------|---------------------| | 0 ... 30 secs | less than a minute | | 30 secs ... 1 min 30 secs | 1 minute | | 1 min 30 secs ... 44 mins 30 secs | [2..44] minutes | | 44 mins ... 30 secs ... 89 mins 30 secs | about 1 hour | | 89 mins 30 secs ... 23 hrs 59 mins 30 secs | about [2..24] hours | | 23 hrs 59 mins 30 secs ... 41 hrs 59 mins 30 secs | 1 day | | 41 hrs 59 mins 30 secs ... 29 days 23 hrs 59 mins 30 secs | [2..30] days | | 29 days 23 hrs 59 mins 30 secs ... 44 days 23 hrs 59 mins 30 secs | about 1 month | | 44 days 23 hrs 59 mins 30 secs ... 59 days 23 hrs 59 mins 30 secs | about 2 months | | 59 days 23 hrs 59 mins 30 secs ... 1 yr | [2..12] months | | 1 yr ... 1 yr 3 months | about 1 year | | 1 yr 3 months ... 1 yr 9 month s | over 1 year | | 1 yr 9 months ... 2 yrs | almost 2 years | | N yrs ... N yrs 3 months | about N years | | N yrs 3 months ... N yrs 9 months | over N years | | N yrs 9 months ... N+1 yrs | almost N+1 years |
With
options.includeSeconds == true
: | Distance between dates | Result | |------------------------|----------------------| | 0 secs ... 5 secs | less than 5 seconds | | 5 secs ... 10 secs | less than 10 seconds | | 10 secs ... 20 secs | less than 20 seconds | | 20 secs ... 40 secs | half a minute | | 40 secs ... 60 secs | less than a minute | | 60 secs ... 90 secs | 1 minute |Parameter laterDate
The date
Parameter earlierDate
The date to compare with
Parameter options
An object with options
Returns
The distance in words
Throws
date
must not be Invalid DateThrows
baseDate
must not be Invalid DateThrows
options.locale
must containformatDistance
propertyExample 1
// What is the distance between 2 July 2014 and 1 January 2015? const result = formatDistance(new Date(2014, 6, 2), new Date(2015, 0, 1)) //=> '6 months'
Example 2
// What is the distance between 1 January 2015 00:00:15 // and 1 January 2015 00:00:00, including seconds? const result = formatDistance( new Date(2015, 0, 1, 0, 0, 15), new Date(2015, 0, 1, 0, 0, 0), { includeSeconds: true } ) //=> 'less than 20 seconds'
Example 3
// What is the distance from 1 January 2016 // to 1 January 2015, with a suffix? const result = formatDistance(new Date(2015, 0, 1), new Date(2016, 0, 1), { addSuffix: true }) //=> 'about 1 year ago'
Example 4
// What is the distance between 1 August 2016 and 1 January 2015 in Esperanto? import { eoLocale } from 'date-fns/locale/eo' const result = formatDistance(new Date(2016, 7, 1), new Date(2015, 0, 1), { locale: eoLocale }) //=> 'pli ol 1 jaro'
function formatDistanceStrict
formatDistanceStrict: ( laterDate: DateArg<Date> & {}, earlierDate: DateArg<Date> & {}, options?: FormatDistanceStrictOptions) => string;
formatDistanceStrict Common Helpers Return the distance between the given dates in words.
Return the distance between the given dates in words, using strict units. This is like
formatDistance
, but does not use helpers like 'almost', 'over', 'less than' and the like.| Distance between dates | Result | |------------------------|---------------------| | 0 ... 59 secs | [0..59] seconds | | 1 ... 59 mins | [1..59] minutes | | 1 ... 23 hrs | [1..23] hours | | 1 ... 29 days | [1..29] days | | 1 ... 11 months | [1..11] months | | 1 ... N years | [1..N] years |
Parameter laterDate
The date
Parameter earlierDate
The date to compare with
Parameter options
An object with options
Returns
The distance in words
Throws
date
must not be Invalid DateThrows
baseDate
must not be Invalid DateThrows
options.unit
must be 'second', 'minute', 'hour', 'day', 'month' or 'year'Throws
options.locale
must containformatDistance
propertyExample 1
// What is the distance between 2 July 2014 and 1 January 2015? const result = formatDistanceStrict(new Date(2014, 6, 2), new Date(2015, 0, 2)) //=> '6 months'
Example 2
// What is the distance between 1 January 2015 00:00:15 // and 1 January 2015 00:00:00? const result = formatDistanceStrict( new Date(2015, 0, 1, 0, 0, 15), new Date(2015, 0, 1, 0, 0, 0) ) //=> '15 seconds'
Example 3
// What is the distance from 1 January 2016 // to 1 January 2015, with a suffix? const result = formatDistanceStrict(new Date(2015, 0, 1), new Date(2016, 0, 1), { addSuffix: true }) //=> '1 year ago'
Example 4
// What is the distance from 1 January 2016 // to 1 January 2015, in minutes? const result = formatDistanceStrict(new Date(2016, 0, 1), new Date(2015, 0, 1), { unit: 'minute' }) //=> '525600 minutes'
Example 5
// What is the distance from 1 January 2015 // to 28 January 2015, in months, rounded up? const result = formatDistanceStrict(new Date(2015, 0, 28), new Date(2015, 0, 1), { unit: 'month', roundingMethod: 'ceil' }) //=> '1 month'
Example 6
// What is the distance between 1 August 2016 and 1 January 2015 in Esperanto? import { eoLocale } from 'date-fns/locale/eo' const result = formatDistanceStrict(new Date(2016, 7, 1), new Date(2015, 0, 1), { locale: eoLocale }) //=> '1 jaro'
function formatDistanceToNow
formatDistanceToNow: ( date: DateArg<Date> & {}, options?: FormatDistanceToNowOptions) => string;
formatDistanceToNow Common Helpers Return the distance between the given date and now in words. false
Return the distance between the given date and now in words.
| Distance to now | Result | |-------------------------------------------------------------------|---------------------| | 0 ... 30 secs | less than a minute | | 30 secs ... 1 min 30 secs | 1 minute | | 1 min 30 secs ... 44 mins 30 secs | [2..44] minutes | | 44 mins ... 30 secs ... 89 mins 30 secs | about 1 hour | | 89 mins 30 secs ... 23 hrs 59 mins 30 secs | about [2..24] hours | | 23 hrs 59 mins 30 secs ... 41 hrs 59 mins 30 secs | 1 day | | 41 hrs 59 mins 30 secs ... 29 days 23 hrs 59 mins 30 secs | [2..30] days | | 29 days 23 hrs 59 mins 30 secs ... 44 days 23 hrs 59 mins 30 secs | about 1 month | | 44 days 23 hrs 59 mins 30 secs ... 59 days 23 hrs 59 mins 30 secs | about 2 months | | 59 days 23 hrs 59 mins 30 secs ... 1 yr | [2..12] months | | 1 yr ... 1 yr 3 months | about 1 year | | 1 yr 3 months ... 1 yr 9 month s | over 1 year | | 1 yr 9 months ... 2 yrs | almost 2 years | | N yrs ... N yrs 3 months | about N years | | N yrs 3 months ... N yrs 9 months | over N years | | N yrs 9 months ... N+1 yrs | almost N+1 years |
With
options.includeSeconds == true
: | Distance to now | Result | |---------------------|----------------------| | 0 secs ... 5 secs | less than 5 seconds | | 5 secs ... 10 secs | less than 10 seconds | | 10 secs ... 20 secs | less than 20 seconds | | 20 secs ... 40 secs | half a minute | | 40 secs ... 60 secs | less than a minute | | 60 secs ... 90 secs | 1 minute |Parameter date
The given date
Parameter options
The object with options
Returns
The distance in words
Throws
date
must not be Invalid DateThrows
options.locale
must containformatDistance
propertyExample 1
// If today is 1 January 2015, what is the distance to 2 July 2014? const result = formatDistanceToNow( new Date(2014, 6, 2) ) //=> '6 months'
Example 2
// If now is 1 January 2015 00:00:00, // what is the distance to 1 January 2015 00:00:15, including seconds? const result = formatDistanceToNow( new Date(2015, 0, 1, 0, 0, 15), {includeSeconds: true} ) //=> 'less than 20 seconds'
Example 3
// If today is 1 January 2015, // what is the distance to 1 January 2016, with a suffix? const result = formatDistanceToNow( new Date(2016, 0, 1), {addSuffix: true} ) //=> 'in about 1 year'
Example 4
// If today is 1 January 2015, // what is the distance to 1 August 2016 in Esperanto? const eoLocale = require('date-fns/locale/eo') const result = formatDistanceToNow( new Date(2016, 7, 1), {locale: eoLocale} ) //=> 'pli ol 1 jaro'
function formatDistanceToNowStrict
formatDistanceToNowStrict: ( date: DateArg<Date> & {}, options?: FormatDistanceToNowStrictOptions) => string;
formatDistanceToNowStrict Common Helpers Return the distance between the given date and now in words. false
Return the distance between the given dates in words, using strict units. This is like
formatDistance
, but does not use helpers like 'almost', 'over', 'less than' and the like.| Distance between dates | Result | |------------------------|---------------------| | 0 ... 59 secs | [0..59] seconds | | 1 ... 59 mins | [1..59] minutes | | 1 ... 23 hrs | [1..23] hours | | 1 ... 29 days | [1..29] days | | 1 ... 11 months | [1..11] months | | 1 ... N years | [1..N] years |
Parameter date
The given date
Parameter options
An object with options.
Returns
The distance in words
Throws
date
must not be Invalid DateThrows
options.locale
must containformatDistance
propertyExample 1
// If today is 1 January 2015, what is the distance to 2 July 2014? const result = formatDistanceToNowStrict( new Date(2014, 6, 2) ) //=> '6 months'
Example 2
// If now is 1 January 2015 00:00:00, // what is the distance to 1 January 2015 00:00:15, including seconds? const result = formatDistanceToNowStrict( new Date(2015, 0, 1, 0, 0, 15) ) //=> '15 seconds'
Example 3
// If today is 1 January 2015, // what is the distance to 1 January 2016, with a suffix? const result = formatDistanceToNowStrict( new Date(2016, 0, 1), {addSuffix: true} ) //=> 'in 1 year'
Example 4
// If today is 28 January 2015, // what is the distance to 1 January 2015, in months, rounded up?? const result = formatDistanceToNowStrict(new Date(2015, 0, 1), { unit: 'month', roundingMethod: 'ceil' }) //=> '1 month'
Example 5
// If today is 1 January 2015, // what is the distance to 1 January 2016 in Esperanto? const eoLocale = require('date-fns/locale/eo') const result = formatDistanceToNowStrict( new Date(2016, 0, 1), {locale: eoLocale} ) //=> '1 jaro'
function formatDuration
formatDuration: (duration: Duration, options?: FormatDurationOptions) => string;
formatDuration Common Helpers Formats a duration in human-readable format
Return human-readable duration string i.e. "9 months 2 days"
Parameter duration
The duration to format
Parameter options
An object with options.
Returns
The formatted date string
Example 1
// Format full duration formatDuration({ years: 2, months: 9, weeks: 1, days: 7, hours: 5, minutes: 9, seconds: 30 }) //=> '2 years 9 months 1 week 7 days 5 hours 9 minutes 30 seconds'
Example 2
// Format partial duration formatDuration({ months: 9, days: 2 }) //=> '9 months 2 days'
Example 3
// Customize the format formatDuration( { years: 2, months: 9, weeks: 1, days: 7, hours: 5, minutes: 9, seconds: 30 }, { format: ['months', 'weeks'] } ) === '9 months 1 week'
Example 4
// Customize the zeros presence formatDuration({ years: 0, months: 9 }) //=> '9 months' formatDuration({ years: 0, months: 9 }, { zero: true }) //=> '0 years 9 months'
Example 5
// Customize the delimiter formatDuration({ years: 2, months: 9, weeks: 3 }, { delimiter: ', ' }) //=> '2 years, 9 months, 3 weeks'
function formatISO
formatISO: (date: DateArg<Date> & {}, options?: FormatISOOptions) => string;
formatISO Common Helpers Format the date according to the ISO 8601 standard (https://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a003169814.htm).
Return the formatted date string in ISO 8601 format. Options may be passed to control the parts and notations of the date.
Parameter date
The original date
Parameter options
An object with options.
Returns
The formatted date string (in local time zone)
Throws
date
must not be Invalid DateExample 1
// Represent 18 September 2019 in ISO 8601 format (local time zone is UTC): const result = formatISO(new Date(2019, 8, 18, 19, 0, 52)) //=> '2019-09-18T19:00:52Z'
Example 2
// Represent 18 September 2019 in ISO 8601, short format (local time zone is UTC): const result = formatISO(new Date(2019, 8, 18, 19, 0, 52), { format: 'basic' }) //=> '20190918T190052'
Example 3
// Represent 18 September 2019 in ISO 8601 format, date only: const result = formatISO(new Date(2019, 8, 18, 19, 0, 52), { representation: 'date' }) //=> '2019-09-18'
Example 4
// Represent 18 September 2019 in ISO 8601 format, time only (local time zone is UTC): const result = formatISO(new Date(2019, 8, 18, 19, 0, 52), { representation: 'time' }) //=> '19:00:52Z'
function formatISO9075
formatISO9075: ( date: DateArg<Date> & {}, options?: FormatISO9075Options) => string;
formatISO9075 Common Helpers Format the date according to the ISO 9075 standard (https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_get-format).
Return the formatted date string in ISO 9075 format. Options may be passed to control the parts and notations of the date.
Parameter date
The original date
Parameter options
An object with options.
Returns
The formatted date string
Throws
date
must not be Invalid DateExample 1
// Represent 18 September 2019 in ISO 9075 format: const result = formatISO9075(new Date(2019, 8, 18, 19, 0, 52)) //=> '2019-09-18 19:00:52'
Example 2
// Represent 18 September 2019 in ISO 9075, short format: const result = formatISO9075(new Date(2019, 8, 18, 19, 0, 52), { format: 'basic' }) //=> '20190918 190052'
Example 3
// Represent 18 September 2019 in ISO 9075 format, date only: const result = formatISO9075(new Date(2019, 8, 18, 19, 0, 52), { representation: 'date' }) //=> '2019-09-18'
Example 4
// Represent 18 September 2019 in ISO 9075 format, time only: const result = formatISO9075(new Date(2019, 8, 18, 19, 0, 52), { representation: 'time' }) //=> '19:00:52'
function formatISODuration
formatISODuration: (duration: Duration) => string;
formatISODuration Common Helpers Format a duration object according as ISO 8601 duration string
Format a duration object according to the ISO 8601 duration standard (https://www.digi.com/resources/documentation/digidocs//90001488-13/reference/r_iso_8601_duration_format.htm)
Parameter duration
The duration to format
Returns
The ISO 8601 duration string
Example 1
// Format the given duration as ISO 8601 string const result = formatISODuration({ years: 39, months: 2, days: 20, hours: 7, minutes: 5, seconds: 0 }) //=> 'P39Y2M20DT0H0M0S'
function formatRelative
formatRelative: ( date: DateArg<Date> & {}, baseDate: DateArg<Date> & {}, options?: FormatRelativeOptions) => string;
formatRelative Common Helpers Represent the date in words relative to the given base date.
Represent the date in words relative to the given base date.
| Distance to the base date | Result | |---------------------------|---------------------------| | Previous 6 days | last Sunday at 04:30 AM | | Last day | yesterday at 04:30 AM | | Same day | today at 04:30 AM | | Next day | tomorrow at 04:30 AM | | Next 6 days | Sunday at 04:30 AM | | Other | 12/31/2017 |
Parameter date
The date to format
Parameter baseDate
The date to compare with
Parameter options
An object with options
Returns
The date in words
Throws
date
must not be Invalid DateThrows
baseDate
must not be Invalid DateThrows
options.locale
must containlocalize
propertyThrows
options.locale
must containformatLong
propertyThrows
options.locale
must containformatRelative
propertyExample 1
// Represent the date of 6 days ago in words relative to the given base date. In this example, today is Wednesday const result = formatRelative(subDays(new Date(), 6), new Date()) //=> "last Thursday at 12:45 AM"
function formatRFC3339
formatRFC3339: ( date: DateArg<Date> & {}, options?: FormatRFC3339Options) => string;
formatRFC3339 Common Helpers Format the date according to the RFC 3339 standard (https://tools.ietf.org/html/rfc3339#section-5.6).
Return the formatted date string in RFC 3339 format. Options may be passed to control the parts and notations of the date.
Parameter date
The original date
Parameter options
An object with options.
Returns
The formatted date string
Throws
date
must not be Invalid DateExample 1
// Represent 18 September 2019 in RFC 3339 format: formatRFC3339(new Date(2019, 8, 18, 19, 0, 52)) //=> '2019-09-18T19:00:52Z'
Example 2
// Represent 18 September 2019 in RFC 3339 format, 3 digits of second fraction formatRFC3339(new Date(2019, 8, 18, 19, 0, 52, 234), { fractionDigits: 3 }) //=> '2019-09-18T19:00:52.234Z'
function formatRFC7231
formatRFC7231: (date: DateArg<Date> & {}) => string;
formatRFC7231 Common Helpers Format the date according to the RFC 7231 standard (https://tools.ietf.org/html/rfc7231#section-7.1.1.1).
Return the formatted date string in RFC 7231 format. The result will always be in UTC timezone.
Parameter date
The original date
Returns
The formatted date string
Throws
date
must not be Invalid DateExample 1
// Represent 18 September 2019 in RFC 7231 format: const result = formatRFC7231(new Date(2019, 8, 18, 19, 0, 52)) //=> 'Wed, 18 Sep 2019 19:00:52 GMT'
function fromUnixTime
fromUnixTime: <DateType extends Date = Date>( unixTime: number, options?: FromUnixTimeOptions<DateType> | undefined) => DateType;
fromUnixTime Timestamp Helpers Create a date from a Unix timestamp.
Create a date from a Unix timestamp (in seconds). Decimal values will be discarded.
Parameter unixTime
The given Unix timestamp (in seconds)
Parameter options
An object with options. Allows to pass a context.
Returns
The date
Example 1
// Create the date 29 February 2012 11:45:05: const result = fromUnixTime(1330515905) //=> Wed Feb 29 2012 11:45:05
function getDate
getDate: ( date: DateArg<Date> & {}, options?: GetDateOptions | undefined) => number;
getDate Day Helpers Get the day of the month of the given date.
Get the day of the month of the given date.
Parameter date
The given date
Parameter options
An object with options.
Returns
The day of month
Example 1
// Which day of the month is 29 February 2012? const result = getDate(new Date(2012, 1, 29)) //=> 29
function getDay
getDay: ( date: DateArg<Date> & {}, options?: GetDayOptions | undefined) => number;
getDay Weekday Helpers Get the day of the week of the given date.
Get the day of the week of the given date.
Parameter date
The given date
Parameter options
The options
Returns
The day of week, 0 represents Sunday
Example 1
// Which day of the week is 29 February 2012? const result = getDay(new Date(2012, 1, 29)) //=> 3
function getDayOfYear
getDayOfYear: ( date: DateArg<Date> & {}, options?: GetDayOfYearOptions | undefined) => number;
getDayOfYear Day Helpers Get the day of the year of the given date.
Get the day of the year of the given date.
Parameter date
The given date
Parameter options
The options
Returns
The day of year
Example 1
// Which day of the year is 2 July 2014? const result = getDayOfYear(new Date(2014, 6, 2)) //=> 183
function getDaysInMonth
getDaysInMonth: ( date: DateArg<Date> & {}, options?: GetDaysInMonthOptions | undefined) => number;
getDaysInMonth Month Helpers Get the number of days in a month of the given date.
Get the number of days in a month of the given date, considering the context if provided.
Parameter date
The given date
Parameter options
An object with options
Returns
The number of days in a month
Example 1
// How many days are in February 2000? const result = getDaysInMonth(new Date(2000, 1)) //=> 29
function getDaysInYear
getDaysInYear: ( date: DateArg<Date> & {}, options?: GetDaysInYearOptions | undefined) => number;
getDaysInYear Year Helpers Get the number of days in a year of the given date.
Get the number of days in a year of the given date.
Parameter date
The given date
Parameter options
An object with options
Returns
The number of days in a year
Example 1
// How many days are in 2012? const result = getDaysInYear(new Date(2012, 0, 1)) //=> 366
function getDecade
getDecade: ( date: DateArg<Date> & {}, options?: GetDecadeOptions | undefined) => number;
getDecade Decade Helpers Get the decade of the given date.
Get the decade of the given date.
Parameter date
The given date
Parameter options
An object with options
Returns
The year of decade
Example 1
// Which decade belongs 27 November 1942? const result = getDecade(new Date(1942, 10, 27)) //=> 1940
function getDefaultOptions
getDefaultOptions: () => DefaultOptions;
getDefaultOptions Common Helpers Get default options. false
Returns an object that contains defaults for
options.locale
,options.weekStartsOn
andoptions.firstWeekContainsDate
arguments for all functions.You can change these with [setDefaultOptions](https://date-fns.org/docs/setDefaultOptions).
Returns
The default options
Example 1
const result = getDefaultOptions() //=> {}
Example 2
setDefaultOptions({ weekStarsOn: 1, firstWeekContainsDate: 4 }) const result = getDefaultOptions() //=> { weekStarsOn: 1, firstWeekContainsDate: 4 }
function getHours
getHours: ( date: DateArg<Date> & {}, options?: GetHoursOptions | undefined) => number;
getHours Hour Helpers Get the hours of the given date.
Get the hours of the given date.
Parameter date
The given date
Parameter options
An object with options
Returns
The hours
Example 1
// Get the hours of 29 February 2012 11:45:00: const result = getHours(new Date(2012, 1, 29, 11, 45)) //=> 11
function getISODay
getISODay: (date: DateArg<Date> & {}, options?: GetISODayOptions) => number;
getISODay Weekday Helpers Get the day of the ISO week of the given date.
Get the day of the ISO week of the given date, which is 7 for Sunday, 1 for Monday etc.
ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_date
Parameter date
The given date
Parameter options
An object with options
Returns
The day of ISO week
Example 1
// Which day of the ISO week is 26 February 2012? const result = getISODay(new Date(2012, 1, 26)) //=> 7
function getISOWeek
getISOWeek: ( date: DateArg<Date> & {}, options?: GetISOWeekOptions | undefined) => number;
getISOWeek ISO Week Helpers Get the ISO week of the given date.
Get the ISO week of the given date.
ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_date
Parameter date
The given date
Parameter options
The options
Returns
The ISO week
Example 1
// Which week of the ISO-week numbering year is 2 January 2005? const result = getISOWeek(new Date(2005, 0, 2)) //=> 53
function getISOWeeksInYear
getISOWeeksInYear: ( date: DateArg<Date> & {}, options?: GetISOWeeksInYearOptions | undefined) => number;
getISOWeeksInYear ISO Week-Numbering Year Helpers Get the number of weeks in an ISO week-numbering year of the given date.
Get the number of weeks in an ISO week-numbering year of the given date.
ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_date
Parameter date
The given date
Parameter options
An object with options
Returns
The number of ISO weeks in a year
Example 1
// How many weeks are in ISO week-numbering year 2015? const result = getISOWeeksInYear(new Date(2015, 1, 11)) //=> 53
function getISOWeekYear
getISOWeekYear: ( date: DateArg<Date> & {}, options?: GetISOWeekYearOptions | undefined) => number;
getISOWeekYear ISO Week-Numbering Year Helpers Get the ISO week-numbering year of the given date.
Get the ISO week-numbering year of the given date, which always starts 3 days before the year's first Thursday.
ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_date
Parameter date
The given date
Returns
The ISO week-numbering year
Example 1
// Which ISO-week numbering year is 2 January 2005? const result = getISOWeekYear(new Date(2005, 0, 2)) //=> 2004
function getMilliseconds
getMilliseconds: (date: DateArg<Date> & {}) => number;
getMilliseconds Millisecond Helpers Get the milliseconds of the given date.
Get the milliseconds of the given date.
Parameter date
The given date
Returns
The milliseconds
Example 1
// Get the milliseconds of 29 February 2012 11:45:05.123: const result = getMilliseconds(new Date(2012, 1, 29, 11, 45, 5, 123)) //=> 123
function getMinutes
getMinutes: ( date: DateArg<Date> & {}, options?: GetMinutesOptions | undefined) => number;
getMinutes Minute Helpers Get the minutes of the given date.
Get the minutes of the given date.
Parameter date
The given date
Parameter options
The options
Returns
The minutes
Example 1
// Get the minutes of 29 February 2012 11:45:05: const result = getMinutes(new Date(2012, 1, 29, 11, 45, 5)) //=> 45
function getMonth
getMonth: ( date: DateArg<Date> & {}, options?: GetMonthOptions | undefined) => number;
getMonth Month Helpers Get the month of the given date.
Get the month of the given date.
Parameter date
The given date
Parameter options
An object with options
Returns
The month index (0-11)
Example 1
// Which month is 29 February 2012? const result = getMonth(new Date(2012, 1, 29)) //=> 1
function getOverlappingDaysInIntervals
getOverlappingDaysInIntervals: ( intervalLeft: Interval, intervalRight: Interval) => number;
getOverlappingDaysInIntervals Interval Helpers Get the number of days that overlap in two time intervals
Get the number of days that overlap in two time intervals. It uses the time between dates to calculate the number of days, rounding it up to include partial days.
Two equal 0-length intervals will result in 0. Two equal 1ms intervals will result in 1.
Parameter intervalLeft
The first interval to compare.
Parameter intervalRight
The second interval to compare.
Parameter options
An object with options
Returns
The number of days that overlap in two time intervals
Example 1
// For overlapping time intervals adds 1 for each started overlapping day: getOverlappingDaysInIntervals( { start: new Date(2014, 0, 10), end: new Date(2014, 0, 20) }, { start: new Date(2014, 0, 17), end: new Date(2014, 0, 21) } ) //=> 3
Example 2
// For non-overlapping time intervals returns 0: getOverlappingDaysInIntervals( { start: new Date(2014, 0, 10), end: new Date(2014, 0, 20) }, { start: new Date(2014, 0, 21), end: new Date(2014, 0, 22) } ) //=> 0
function getQuarter
getQuarter: ( date: DateArg<Date> & {}, options?: GetQuarterOptions | undefined) => number;
getQuarter Quarter Helpers Get the year quarter of the given date.
Get the year quarter of the given date.
Parameter date
The given date
Parameter options
An object with options
Returns
The quarter
Example 1
// Which quarter is 2 July 2014? const result = getQuarter(new Date(2014, 6, 2)); //=> 3
function getSeconds
getSeconds: (date: DateArg<Date> & {}) => number;
getSeconds Second Helpers Get the seconds of the given date.
Get the seconds of the given date.
Parameter date
The given date
Returns
The seconds
Example 1
// Get the seconds of 29 February 2012 11:45:05.123: const result = getSeconds(new Date(2012, 1, 29, 11, 45, 5, 123)) //=> 5
function getTime
getTime: (date: DateArg<Date> & {}) => number;
getTime Timestamp Helpers Get the milliseconds timestamp of the given date.
Get the milliseconds timestamp of the given date.
Parameter date
The given date
Returns
The timestamp
Example 1
// Get the timestamp of 29 February 2012 11:45:05.123: const result = getTime(new Date(2012, 1, 29, 11, 45, 5, 123)) //=> 1330515905123
function getUnixTime
getUnixTime: (date: DateArg<Date> & {}) => number;
getUnixTime Timestamp Helpers Get the seconds timestamp of the given date.
Get the seconds timestamp of the given date.
Parameter date
The given date
Returns
The timestamp
Example 1
// Get the timestamp of 29 February 2012 11:45:05 CET: const result = getUnixTime(new Date(2012, 1, 29, 11, 45, 5)) //=> 1330512305
function getWeek
getWeek: ( date: DateArg<Date> & {}, options?: GetWeekOptions | undefined) => number;
getWeek Week Helpers Get the local week index of the given date.
Get the local week index of the given date. The exact calculation depends on the values of
options.weekStartsOn
(which is the index of the first day of the week) andoptions.firstWeekContainsDate
(which is the day of January, which is always in the first week of the week-numbering year)Week numbering: https://en.wikipedia.org/wiki/Week#The_ISO_week_date_system
Parameter date
The given date
Parameter options
An object with options
Returns
The week
Example 1
// Which week of the local week numbering year is 2 January 2005 with default options? const result = getWeek(new Date(2005, 0, 2)) //=> 2
Example 2
// Which week of the local week numbering year is 2 January 2005, // if Monday is the first day of the week, // and the first week of the year always contains 4 January? const result = getWeek(new Date(2005, 0, 2), { weekStartsOn: 1, firstWeekContainsDate: 4 }) //=> 53
function getWeekOfMonth
getWeekOfMonth: ( date: DateArg<Date> & {}, options?: GetWeekOfMonthOptions) => number;
getWeekOfMonth Week Helpers Get the week of the month of the given date.
Get the week of the month of the given date.
Parameter date
The given date
Parameter options
An object with options.
Returns
The week of month
Example 1
// Which week of the month is 9 November 2017? const result = getWeekOfMonth(new Date(2017, 10, 9)) //=> 2
function getWeeksInMonth
getWeeksInMonth: ( date: DateArg<Date> & {}, options?: GetWeeksInMonthOptions | undefined) => number;
getWeeksInMonth Week Helpers Get the number of calendar weeks a month spans.
Get the number of calendar weeks the month in the given date spans.
Parameter date
The given date
Parameter options
An object with options.
Returns
The number of calendar weeks
Example 1
// How many calendar weeks does February 2015 span? const result = getWeeksInMonth(new Date(2015, 1, 8)) //=> 4
Example 2
// If the week starts on Monday, // how many calendar weeks does July 2017 span? const result = getWeeksInMonth(new Date(2017, 6, 5), { weekStartsOn: 1 }) //=> 6
function getWeekYear
getWeekYear: (date: DateArg<Date> & {}, options?: GetWeekYearOptions) => number;
getWeekYear Week-Numbering Year Helpers Get the local week-numbering year of the given date.
Get the local week-numbering year of the given date. The exact calculation depends on the values of
options.weekStartsOn
(which is the index of the first day of the week) andoptions.firstWeekContainsDate
(which is the day of January, which is always in the first week of the week-numbering year)Week numbering: https://en.wikipedia.org/wiki/Week#The_ISO_week_date_system
Parameter date
The given date
Parameter options
An object with options.
Returns
The local week-numbering year
Example 1
// Which week numbering year is 26 December 2004 with the default settings? const result = getWeekYear(new Date(2004, 11, 26)) //=> 2005
Example 2
// Which week numbering year is 26 December 2004 if week starts on Saturday? const result = getWeekYear(new Date(2004, 11, 26), { weekStartsOn: 6 }) //=> 2004
Example 3
// Which week numbering year is 26 December 2004 if the first week contains 4 January? const result = getWeekYear(new Date(2004, 11, 26), { firstWeekContainsDate: 4 }) //=> 2004
function getYear
getYear: ( date: DateArg<Date> & {}, options?: GetYearOptions | undefined) => number;
getYear Year Helpers Get the year of the given date.
Get the year of the given date.
Parameter date
The given date
Parameter options
An object with options
Returns
The year
Example 1
// Which year is 2 July 2014? const result = getYear(new Date(2014, 6, 2)) //=> 2014
function hoursToMilliseconds
hoursToMilliseconds: (hours: number) => number;
hoursToMilliseconds Conversion Helpers Convert hours to milliseconds.
Convert a number of hours to a full number of milliseconds.
Parameter hours
number of hours to be converted
Returns
The number of hours converted to milliseconds
Example 1
// Convert 2 hours to milliseconds: const result = hoursToMilliseconds(2) //=> 7200000
function hoursToMinutes
hoursToMinutes: (hours: number) => number;
hoursToMinutes Conversion Helpers Convert hours to minutes.
Convert a number of hours to a full number of minutes.
Parameter hours
number of hours to be converted
Returns
The number of hours converted in minutes
Example 1
// Convert 2 hours to minutes: const result = hoursToMinutes(2) //=> 120
function hoursToSeconds
hoursToSeconds: (hours: number) => number;
hoursToSeconds Conversion Helpers Convert hours to seconds.
Convert a number of hours to a full number of seconds.
Parameter hours
The number of hours to be converted
Returns
The number of hours converted in seconds
Example 1
// Convert 2 hours to seconds: const result = hoursToSeconds(2) //=> 7200
function interval
interval: < StartDate extends DateArg<Date>, EndDate extends DateArg<Date>, Options extends IntervalOptions<Date> = undefined>( start: StartDate, end: EndDate, options?: Options) => IntervalResult<StartDate, EndDate, Options>;
interval Interval Helpers Creates an interval object and validates its values.
Creates a normalized interval object and validates its values. If the interval is invalid, an exception is thrown.
Parameter start
The start of the interval.
Parameter end
The end of the interval.
Parameter options
The options object.
Returns
The normalized and validated interval object.
Throws
Start date is invalid
whenstart
is invalid.Throws
End date is invalid
whenend
is invalid.Throws
End date must be after start date
when end is beforestart
andoptions.assertPositive
is true.
function intervalToDuration
intervalToDuration: ( interval: Interval, options?: IntervalToDurationOptions | undefined) => Duration;
intervalToDuration Common Helpers Convert interval to duration
Convert an interval object to a duration object.
Parameter interval
The interval to convert to duration
Parameter options
The context options
Returns
The duration object
Example 1
// Get the duration between January 15, 1929 and April 4, 1968. intervalToDuration({ start: new Date(1929, 0, 15, 12, 0, 0), end: new Date(1968, 3, 4, 19, 5, 0) }); //=> { years: 39, months: 2, days: 20, hours: 7, minutes: 5, seconds: 0 }
function intlFormat
intlFormat: { (date: DateArg<Date> & {}): string; (date: DateArg<Date> & {}, localeOptions: IntlFormatLocaleOptions): string; (date: DateArg<Date> & {}, formatOptions: Intl.DateTimeFormatOptions): string; ( date: DateArg<Date> & {}, formatOptions: Intl.DateTimeFormatOptions, localeOptions: IntlFormatLocaleOptions ): string;};
intlFormat Common Helpers Format the date with Intl.DateTimeFormat (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat).
Return the formatted date string in the given format. The method uses [
Intl.DateTimeFormat
](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat) inside. formatOptions are the same as [Intl.DateTimeFormat
options](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat#using_options)> ⚠️ Please note that before Node version 13.0.0, only the locale data for en-US is available by default.
Parameter date
The date to format
Returns
The formatted date string
Throws
date
must not be Invalid DateExample 1
// Represent 4 October 2019 in middle-endian format: const result = intlFormat(new Date(2019, 9, 4, 12, 30, 13, 456)) //=> 10/4/2019
Parameter date
The date to format
Parameter localeOptions
An object with locale
Returns
The formatted date string
Throws
date
must not be Invalid DateExample 1
// Represent 4 October 2019 in Korean. // Convert the date with locale's options. const result = intlFormat(new Date(2019, 9, 4, 12, 30, 13, 456), { locale: 'ko-KR', }) //=> 2019. 10. 4.
Parameter date
The date to format
Parameter formatOptions
The format options
Returns
The formatted date string
Throws
date
must not be Invalid DateExample 1
// Represent 4 October 2019. // Convert the date with format's options. const result = intlFormat.default(new Date(2019, 9, 4, 12, 30, 13, 456), { year: 'numeric', month: 'numeric', day: 'numeric', hour: 'numeric', }) //=> 10/4/2019, 12 PM
Parameter date
The date to format
Parameter formatOptions
The format options
Parameter localeOptions
An object with locale
Returns
The formatted date string
Throws
date
must not be Invalid DateExample 1
// Represent 4 October 2019 in German. // Convert the date with format's options and locale's options. const result = intlFormat(new Date(2019, 9, 4, 12, 30, 13, 456), { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric', }, { locale: 'de-DE', }) //=> Freitag, 4. Oktober 2019
function intlFormatDistance
intlFormatDistance: ( laterDate: DateArg<Date> & {}, earlierDate: DateArg<Date> & {}, options?: IntlFormatDistanceOptions) => string;
intlFormatDistance Common Helpers Formats distance between two dates in a human-readable format The function calculates the difference between two dates and formats it as a human-readable string.
The function will pick the most appropriate unit depending on the distance between dates. For example, if the distance is a few hours, it might return
x hours
. If the distance is a few months, it might returnx months
.You can also specify a unit to force using it regardless of the distance to get a result like
123456 hours
.See the table below for the unit picking logic:
| Distance between dates | Result (past) | Result (future) | | ---------------------- | -------------- | --------------- | | 0 seconds | now | now | | 1-59 seconds | X seconds ago | in X seconds | | 1-59 minutes | X minutes ago | in X minutes | | 1-23 hours | X hours ago | in X hours | | 1 day | yesterday | tomorrow | | 2-6 days | X days ago | in X days | | 7 days | last week | next week | | 8 days-1 month | X weeks ago | in X weeks | | 1 month | last month | next month | | 2-3 months | X months ago | in X months | | 1 quarter | last quarter | next quarter | | 2-3 quarters | X quarters ago | in X quarters | | 1 year | last year | next year | | 2+ years | X years ago | in X years |
Parameter laterDate
The date
Parameter earlierDate
The date to compare with.
Parameter options
An object with options. See MDN for details [Locale identification and negotiation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#locale_identification_and_negotiation) The narrow one could be similar to the short one for some locales.
Returns
The distance in words according to language-sensitive relative time formatting.
Throws
date
must not be Invalid DateThrows
baseDate
must not be Invalid DateThrows
options.unit
must not be invalid UnitThrows
options.locale
must not be invalid localeThrows
options.localeMatcher
must not be invalid localeMatcherThrows
options.numeric
must not be invalid numericThrows
options.style
must not be invalid styleExample 1
// What is the distance between the dates when the fist date is after the second? intlFormatDistance( new Date(1986, 3, 4, 11, 30, 0), new Date(1986, 3, 4, 10, 30, 0) ) //=> 'in 1 hour'
// What is the distance between the dates when the fist date is before the second? intlFormatDistance( new Date(1986, 3, 4, 10, 30, 0), new Date(1986, 3, 4, 11, 30, 0) ) //=> '1 hour ago'
Example 2
// Use the unit option to force the function to output the result in quarters. Without setting it, the example would return "next year" intlFormatDistance( new Date(1987, 6, 4, 10, 30, 0), new Date(1986, 3, 4, 10, 30, 0), { unit: 'quarter' } ) //=> 'in 5 quarters'
Example 3
// Use the locale option to get the result in Spanish. Without setting it, the example would return "in 1 hour". intlFormatDistance( new Date(1986, 3, 4, 11, 30, 0), new Date(1986, 3, 4, 10, 30, 0), { locale: 'es' } ) //=> 'dentro de 1 hora'
Example 4
// Use the numeric option to force the function to use numeric values. Without setting it, the example would return "tomorrow". intlFormatDistance( new Date(1986, 3, 5, 11, 30, 0), new Date(1986, 3, 4, 11, 30, 0), { numeric: 'always' } ) //=> 'in 1 day'
Example 5
// Use the style option to force the function to use short values. Without setting it, the example would return "in 2 years". intlFormatDistance( new Date(1988, 3, 4, 11, 30, 0), new Date(1986, 3, 4, 11, 30, 0), { style: 'short' } ) //=> 'in 2 yr'
function isAfter
isAfter: ( date: DateArg<Date> & {}, dateToCompare: DateArg<Date> & {}) => boolean;
isAfter Common Helpers Is the first date after the second one?
Is the first date after the second one?
Parameter date
The date that should be after the other one to return true
Parameter dateToCompare
The date to compare with
Returns
The first date is after the second date
Example 1
// Is 10 July 1989 after 11 February 1987? const result = isAfter(new Date(1989, 6, 10), new Date(1987, 1, 11)) //=> true
function isBefore
isBefore: ( date: DateArg<Date> & {}, dateToCompare: DateArg<Date> & {}) => boolean;
isBefore Common Helpers Is the first date before the second one?
Is the first date before the second one?
Parameter date
The date that should be before the other one to return true
Parameter dateToCompare
The date to compare with
Returns
The first date is before the second date
Example 1
// Is 10 July 1989 before 11 February 1987? const result = isBefore(new Date(1989, 6, 10), new Date(1987, 1, 11)) //=> false
function isDate
isDate: (value: unknown) => value is Date;
isDate Common Helpers Is the given value a date?
Returns true if the given value is an instance of Date. The function works for dates transferred across iframes.
Parameter value
The value to check
Returns
True if the given value is a date
Example 1
// For a valid date: const result = isDate(new Date()) //=> true
Example 2
// For an invalid date: const result = isDate(new Date(NaN)) //=> true
Example 3
// For some value: const result = isDate('2014-02-31') //=> false
Example 4
// For an object: const result = isDate({}) //=> false
function isEqual
isEqual: ( leftDate: DateArg<Date> & {}, rightDate: DateArg<Date> & {}) => boolean;
isEqual Common Helpers Are the given dates equal?
Are the given dates equal?
Parameter dateLeft
The first date to compare
Parameter dateRight
The second date to compare
Returns
The dates are equal
Example 1
// Are 2 July 2014 06:30:45.000 and 2 July 2014 06:30:45.500 equal? const result = isEqual( new Date(2014, 6, 2, 6, 30, 45, 0), new Date(2014, 6, 2, 6, 30, 45, 500) ) //=> false
function isExists
isExists: (year: number, month: number, day: number) => boolean;
isExists Common Helpers Is the given date exists?
Checks if the given arguments convert to an existing date.
Parameter year
The year of the date to check
Parameter month
The month of the date to check
Parameter day
The day of the date to check
Returns
true
if the date existsExample 1
// For the valid date: const result = isExists(2018, 0, 31) //=> true
Example 2
// For the invalid date: const result = isExists(2018, 1, 31) //=> false
function isFirstDayOfMonth
isFirstDayOfMonth: ( date: DateArg<Date> & {}, options?: IsFirstDayOfMonthOptions | undefined) => boolean;
isFirstDayOfMonth Month Helpers Is the given date the first day of a month?
Is the given date the first day of a month?
Parameter date
The date to check
Parameter options
An object with options
Returns
The date is the first day of a month
Example 1
// Is 1 September 2014 the first day of a month? const result = isFirstDayOfMonth(new Date(2014, 8, 1)) //=> true
function isFriday
isFriday: ( date: DateArg<Date> & {}, options?: IsFridayOptions | undefined) => boolean;
isFriday Weekday Helpers Is the given date Friday?
Is the given date Friday?
Parameter date
The date to check
Parameter options
An object with options
Returns
The date is Friday
Example 1
// Is 26 September 2014 Friday? const result = isFriday(new Date(2014, 8, 26)) //=> true
function isFuture
isFuture: (date: DateArg<Date> & {}) => boolean;
isFuture Common Helpers Is the given date in the future? false
Is the given date in the future?
Parameter date
The date to check
Returns
The date is in the future
Example 1
// If today is 6 October 2014, is 31 December 2014 in the future? const result = isFuture(new Date(2014, 11, 31)) //=> true
function isLastDayOfMonth
isLastDayOfMonth: ( date: DateArg<Date> & {}, options?: IsLastDayOfMonthOptions | undefined) => boolean;
isLastDayOfMonth Month Helpers Is the given date the last day of a month?
Is the given date the last day of a month?
Parameter date
The date to check
Parameter options
An object with options
Returns
The date is the last day of a month
Example 1
// Is 28 February 2014 the last day of a month? const result = isLastDayOfMonth(new Date(2014, 1, 28)) //=> true
function isLeapYear
isLeapYear: ( date: DateArg<Date> & {}, options?: IsLeapYearOptions | undefined) => boolean;
isLeapYear Year Helpers Is the given date in the leap year?
Is the given date in the leap year?
Parameter date
The date to check
Parameter options
The options object
Returns
The date is in the leap year
Example 1
// Is 1 September 2012 in the leap year? const result = isLeapYear(new Date(2012, 8, 1)) //=> true
function isMatch
isMatch: ( dateStr: string, formatStr: string, options?: IsMatchOptions) => boolean;
isMatch Common Helpers validates the date string against given formats
Return the true if given date is string correct against the given format else will return false.
> ⚠️ Please note that the
format
tokens differ from Moment.js and other libraries. > See: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.mdThe characters in the format string wrapped between two single quotes characters (') are escaped. Two single quotes in a row, whether inside or outside a quoted sequence, represent a 'real' single quote.
Format of the format string is based on Unicode Technical Standard #35: https://www.unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table with a few additions (see note 5 below the table).
Not all tokens are compatible. Combinations that don't make sense or could lead to bugs are prohibited and will throw
RangeError
. For example usage of 24-hour format token with AM/PM token will throw an exception:isMatch('23 AM', 'HH a')//=> RangeError: The format string mustn't contain `HH` and `a` at the same timeSee the compatibility table: https://docs.google.com/spreadsheets/d/e/2PACX-1vQOPU3xUhplll6dyoMmVUXHKl_8CRDs6_ueLmex3SoqwhuolkuN3O05l4rqx5h1dKX8eb46Ul-CCSrq/pubhtml?gid=0&single=true
Accepted format string patterns: | Unit |Prior| Pattern | Result examples | Notes | |---------------------------------|-----|---------|-----------------------------------|-------| | Era | 140 | G..GGG | AD, BC | | | | | GGGG | Anno Domini, Before Christ | 2 | | | | GGGGG | A, B | | | Calendar year | 130 | y | 44, 1, 1900, 2017, 9999 | 4 | | | | yo | 44th, 1st, 1900th, 9999999th | 4,5 | | | | yy | 44, 01, 00, 17 | 4 | | | | yyy | 044, 001, 123, 999 | 4 | | | | yyyy | 0044, 0001, 1900, 2017 | 4 | | | | yyyyy | ... | 2,4 | | Local week-numbering year | 130 | Y | 44, 1, 1900, 2017, 9000 | 4 | | | | Yo | 44th, 1st, 1900th, 9999999th | 4,5 | | | | YY | 44, 01, 00, 17 | 4,6 | | | | YYY | 044, 001, 123, 999 | 4 | | | | YYYY | 0044, 0001, 1900, 2017 | 4,6 | | | | YYYYY | ... | 2,4 | | ISO week-numbering year | 130 | R | -43, 1, 1900, 2017, 9999, -9999 | 4,5 | | | | RR | -43, 01, 00, 17 | 4,5 | | | | RRR | -043, 001, 123, 999, -999 | 4,5 | | | | RRRR | -0043, 0001, 2017, 9999, -9999 | 4,5 | | | | RRRRR | ... | 2,4,5 | | Extended year | 130 | u | -43, 1, 1900, 2017, 9999, -999 | 4 | | | | uu | -43, 01, 99, -99 | 4 | | | | uuu | -043, 001, 123, 999, -999 | 4 | | | | uuuu | -0043, 0001, 2017, 9999, -9999 | 4 | | | | uuuuu | ... | 2,4 | | Quarter (formatting) | 120 | Q | 1, 2, 3, 4 | | | | | Qo | 1st, 2nd, 3rd, 4th | 5 | | | | QQ | 01, 02, 03, 04 | | | | | QQQ | Q1, Q2, Q3, Q4 | | | | | QQQQ | 1st quarter, 2nd quarter, ... | 2 | | | | QQQQQ | 1, 2, 3, 4 | 4 | | Quarter (stand-alone) | 120 | q | 1, 2, 3, 4 | | | | | qo | 1st, 2nd, 3rd, 4th | 5 | | | | qq | 01, 02, 03, 04 | | | | | qqq | Q1, Q2, Q3, Q4 | | | | | qqqq | 1st quarter, 2nd quarter, ... | 2 | | | | qqqqq | 1, 2, 3, 4 | 3 | | Month (formatting) | 110 | M | 1, 2, ..., 12 | | | | | Mo | 1st, 2nd, ..., 12th | 5 | | | | MM | 01, 02, ..., 12 | | | | | MMM | Jan, Feb, ..., Dec | | | | | MMMM | January, February, ..., December | 2 | | | | MMMMM | J, F, ..., D | | | Month (stand-alone) | 110 | L | 1, 2, ..., 12 | | | | | Lo | 1st, 2nd, ..., 12th | 5 | | | | LL | 01, 02, ..., 12 | | | | | LLL | Jan, Feb, ..., Dec | | | | | LLLL | January, February, ..., December | 2 | | | | LLLLL | J, F, ..., D | | | Local week of year | 100 | w | 1, 2, ..., 53 | | | | | wo | 1st, 2nd, ..., 53th | 5 | | | | ww | 01, 02, ..., 53 | | | ISO week of year | 100 | I | 1, 2, ..., 53 | 5 | | | | Io | 1st, 2nd, ..., 53th | 5 | | | | II | 01, 02, ..., 53 | 5 | | Day of month | 90 | d | 1, 2, ..., 31 | | | | | do | 1st, 2nd, ..., 31st | 5 | | | | dd | 01, 02, ..., 31 | | | Day of year | 90 | D | 1, 2, ..., 365, 366 | 7 | | | | Do | 1st, 2nd, ..., 365th, 366th | 5 | | | | DD | 01, 02, ..., 365, 366 | 7 | | | | DDD | 001, 002, ..., 365, 366 | | | | | DDDD | ... | 2 | | Day of week (formatting) | 90 | E..EEE | Mon, Tue, Wed, ..., Su | | | | | EEEE | Monday, Tuesday, ..., Sunday | 2 | | | | EEEEE | M, T, W, T, F, S, S | | | | | EEEEEE | Mo, Tu, We, Th, Fr, Sa, Su | | | ISO day of week (formatting) | 90 | i | 1, 2, 3, ..., 7 | 5 | | | | io | 1st, 2nd, ..., 7th | 5 | | | | ii | 01, 02, ..., 07 | 5 | | | | iii | Mon, Tue, Wed, ..., Su | 5 | | | | iiii | Monday, Tuesday, ..., Sunday | 2,5 | | | | iiiii | M, T, W, T, F, S, S | 5 | | | | iiiiii | Mo, Tu, We, Th, Fr, Sa, Su | 5 | | Local day of week (formatting) | 90 | e | 2, 3, 4, ..., 1 | | | | | eo | 2nd, 3rd, ..., 1st | 5 | | | | ee | 02, 03, ..., 01 | | | | | eee | Mon, Tue, Wed, ..., Su | | | | | eeee | Monday, Tuesday, ..., Sunday | 2 | | | | eeeee | M, T, W, T, F, S, S | | | | | eeeeee | Mo, Tu, We, Th, Fr, Sa, Su | | | Local day of week (stand-alone) | 90 | c | 2, 3, 4, ..., 1 | | | | | co | 2nd, 3rd, ..., 1st | 5 | | | | cc | 02, 03, ..., 01 | | | | | ccc | Mon, Tue, Wed, ..., Su | | | | | cccc | Monday, Tuesday, ..., Sunday | 2 | | | | ccccc | M, T, W, T, F, S, S | | | | | cccccc | Mo, Tu, We, Th, Fr, Sa, Su | | | AM, PM | 80 | a..aaa | AM, PM | | | | | aaaa | a.m., p.m. | 2 | | | | aaaaa | a, p | | | AM, PM, noon, midnight | 80 | b..bbb | AM, PM, noon, midnight | | | | | bbbb | a.m., p.m., noon, midnight | 2 | | | | bbbbb | a, p, n, mi | | | Flexible day period | 80 | B..BBB | at night, in the morning, ... | | | | | BBBB | at night, in the morning, ... | 2 | | | | BBBBB | at night, in the morning, ... | | | Hour [1-12] | 70 | h | 1, 2, ..., 11, 12 | | | | | ho | 1st, 2nd, ..., 11th, 12th | 5 | | | | hh | 01, 02, ..., 11, 12 | | | Hour [0-23] | 70 | H | 0, 1, 2, ..., 23 | | | | | Ho | 0th, 1st, 2nd, ..., 23rd | 5 | | | | HH | 00, 01, 02, ..., 23 | | | Hour [0-11] | 70 | K | 1, 2, ..., 11, 0 | | | | | Ko | 1st, 2nd, ..., 11th, 0th | 5 | | | | KK | 01, 02, ..., 11, 00 | | | Hour [1-24] | 70 | k | 24, 1, 2, ..., 23 | | | | | ko | 24th, 1st, 2nd, ..., 23rd | 5 | | | | kk | 24, 01, 02, ..., 23 | | | Minute | 60 | m | 0, 1, ..., 59 | | | | | mo | 0th, 1st, ..., 59th | 5 | | | | mm | 00, 01, ..., 59 | | | Second | 50 | s | 0, 1, ..., 59 | | | | | so | 0th, 1st, ..., 59th | 5 | | | | ss | 00, 01, ..., 59 | | | Seconds timestamp | 40 | t | 512969520 | | | | | tt | ... | 2 | | Fraction of second | 30 | S | 0, 1, ..., 9 | | | | | SS | 00, 01, ..., 99 | | | | | SSS | 000, 001, ..., 999 | | | | | SSSS | ... | 2 | | Milliseconds timestamp | 20 | T | 512969520900 | | | | | TT | ... | 2 | | Timezone (ISO-8601 w/ Z) | 10 | X | -08, +0530, Z | | | | | XX | -0800, +0530, Z | | | | | XXX | -08:00, +05:30, Z | | | | | XXXX | -0800, +0530, Z, +123456 | 2 | | | | XXXXX | -08:00, +05:30, Z, +12:34:56 | | | Timezone (ISO-8601 w/o Z) | 10 | x | -08, +0530, +00 | | | | | xx | -0800, +0530, +0000 | | | | | xxx | -08:00, +05:30, +00:00 | 2 | | | | xxxx | -0800, +0530, +0000, +123456 | | | | | xxxxx | -08:00, +05:30, +00:00, +12:34:56 | | | Long localized date | NA | P | 05/29/1453 | 5,8 | | | | PP | May 29, 1453 | | | | | PPP | May 29th, 1453 | | | | | PPPP | Sunday, May 29th, 1453 | 2,5,8 | | Long localized time | NA | p | 12:00 AM | 5,8 | | | | pp | 12:00:00 AM | | | Combination of date and time | NA | Pp | 05/29/1453, 12:00 AM | | | | | PPpp | May 29, 1453, 12:00:00 AM | | | | | PPPpp | May 29th, 1453 at ... | | | | | PPPPpp | Sunday, May 29th, 1453 at ... | 2,5,8 | Notes: 1. "Formatting" units (e.g. formatting quarter) in the default en-US locale are the same as "stand-alone" units, but are different in some languages. "Formatting" units are declined according to the rules of the language in the context of a date. "Stand-alone" units are always nominative singular. In
format
function, they will produce different result:format(new Date(2017, 10, 6), 'do LLLL', {locale: cs}) //=> '6. listopad'
format(new Date(2017, 10, 6), 'do MMMM', {locale: cs}) //=> '6. listopadu'
isMatch
will try to match both formatting and stand-alone units interchangeably.2. Any sequence of the identical letters is a pattern, unless it is escaped by the single quote characters (see below). If the sequence is longer than listed in table: - for numerical units (
yyyyyyyy
)isMatch
will try to match a number as wide as the sequence - for text units (MMMMMMMM
)isMatch
will try to match the widest variation of the unit. These variations are marked with "2" in the last column of the table.3.
QQQQQ
andqqqqq
could be not strictly numerical in some locales. These tokens represent the shortest form of the quarter.4. The main difference between
y
andu
patterns are B.C. years:| Year |
y
|u
| |------|-----|-----| | AC 1 | 1 | 1 | | BC 1 | 1 | 0 | | BC 2 | 2 | -1 |Also
yy
will try to guess the century of two digit year by proximity withreferenceDate
:isMatch('50', 'yy') //=> true
isMatch('75', 'yy') //=> true
while
uu
will use the year as is:isMatch('50', 'uu') //=> true
isMatch('75', 'uu') //=> true
The same difference is true for local and ISO week-numbering years (
Y
andR
), except local week-numbering years are dependent onoptions.weekStartsOn
andoptions.firstWeekContainsDate
(compare [setISOWeekYear](https://date-fns.org/docs/setISOWeekYear) and [setWeekYear](https://date-fns.org/docs/setWeekYear)).5. These patterns are not in the Unicode Technical Standard #35: -
i
: ISO day of week -I
: ISO week of year -R
: ISO week-numbering year -o
: ordinal number modifier -P
: long localized date -p
: long localized time6.
YY
andYYYY
tokens represent week-numbering years but they are often confused with years. You should enableoptions.useAdditionalWeekYearTokens
to use them. See: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md7.
D
andDD
tokens represent days of the year but they are often confused with days of the month. You should enableoptions.useAdditionalDayOfYearTokens
to use them. See: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md8.
P+
tokens do not have a defined priority since they are merely aliases to other tokens based on the given locale.using
en-US
locale:P
=>MM/dd/yyyy
usingen-US
locale:p
=>hh:mm a
usingpt-BR
locale:P
=>dd/MM/yyyy
usingpt-BR
locale:p
=>HH:mm
Values will be checked in the descending order of its unit's priority. Units of an equal priority overwrite each other in the order of appearance.
If no values of higher priority are matched (e.g. when matching string 'January 1st' without a year), the values will be taken from today's using
new Date()
date which works as a context of parsing.The result may vary by locale.
If
formatString
matches withdateString
but does not provides tokens,referenceDate
will be returned.Parameter dateStr
The date string to verify
Parameter format
The string of tokens
Parameter options
An object with options. see: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md see: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md
Returns
Is format string a match for date string?
Throws
options.locale
must containmatch
propertyThrows
use
yyyy
instead ofYYYY
for formatting years; see: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.mdThrows
use
yy
instead ofYY
for formatting years; see: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.mdThrows
use
d
instead ofD
for formatting days of the month; see: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.mdThrows
use
dd
instead ofDD
for formatting days of the month; see: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.mdThrows
format string contains an unescaped latin alphabet character
Example 1
// Match 11 February 2014 from middle-endian format: const result = isMatch('02/11/2014', 'MM/dd/yyyy') //=> true
Example 2
// Match 28th of February in Esperanto locale in the context of 2010 year: import eo from 'date-fns/locale/eo' const result = isMatch('28-a de februaro', "do 'de' MMMM", { locale: eo }) //=> true
function isMonday
isMonday: ( date: DateArg<Date> & {}, options?: IsMondayOptions | undefined) => boolean;
isMonday Weekday Helpers Is the given date Monday?
Is the given date Monday?
Parameter date
The date to check
Parameter options
An object with options
Returns
The date is Monday
Example 1
// Is 22 September 2014 Monday? const result = isMonday(new Date(2014, 8, 22)) //=> true
function isPast
isPast: (date: DateArg<Date> & {}) => boolean;
isPast Common Helpers Is the given date in the past? false
Is the given date in the past?
Parameter date
The date to check
Returns
The date is in the past
Example 1
// If today is 6 October 2014, is 2 July 2014 in the past? const result = isPast(new Date(2014, 6, 2)) //=> true
function isSameDay
isSameDay: ( laterDate: DateArg<Date> & {}, earlierDate: DateArg<Date> & {}, options?: IsSameDayOptions | undefined) => boolean;
isSameDay Day Helpers Are the given dates in the same day (and year and month)?
Are the given dates in the same day (and year and month)?
Parameter laterDate
The first date to check
Parameter earlierDate
The second date to check
Parameter options
An object with options
Returns
The dates are in the same day (and year and month)
Example 1
// Are 4 September 06:00:00 and 4 September 18:00:00 in the same day? const result = isSameDay(new Date(2014, 8, 4, 6, 0), new Date(2014, 8, 4, 18, 0)) //=> true
Example 2
// Are 4 September and 4 October in the same day? const result = isSameDay(new Date(2014, 8, 4), new Date(2014, 9, 4)) //=> false
Example 3
// Are 4 September, 2014 and 4 September, 2015 in the same day? const result = isSameDay(new Date(2014, 8, 4), new Date(2015, 8, 4)) //=> false
function isSameHour
isSameHour: ( dateLeft: DateArg<Date> & {}, dateRight: DateArg<Date> & {}, options?: IsSameHourOptions | undefined) => boolean;
isSameHour Hour Helpers Are the given dates in the same hour (and same day)?
Are the given dates in the same hour (and same day)?
Parameter dateLeft
The first date to check
Parameter dateRight
The second date to check
Parameter options
An object with options
Returns
The dates are in the same hour (and same day)
Example 1
// Are 4 September 2014 06:00:00 and 4 September 06:30:00 in the same hour? const result = isSameHour(new Date(2014, 8, 4, 6, 0), new Date(2014, 8, 4, 6, 30)) //=> true
Example 2
// Are 4 September 2014 06:00:00 and 5 September 06:00:00 in the same hour? const result = isSameHour(new Date(2014, 8, 4, 6, 0), new Date(2014, 8, 5, 6, 0)) //=> false
function isSameISOWeek
isSameISOWeek: ( laterDate: DateArg<Date> & {}, earlierDate: DateArg<Date> & {}, options?: IsSameISOWeekOptions | undefined) => boolean;
isSameISOWeek ISO Week Helpers Are the given dates in the same ISO week (and year)?
Are the given dates in the same ISO week (and year)?
ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_date
Parameter laterDate
The first date to check
Parameter earlierDate
The second date to check
Parameter options
An object with options
Returns
The dates are in the same ISO week (and year)
Example 1
// Are 1 September 2014 and 7 September 2014 in the same ISO week? const result = isSameISOWeek(new Date(2014, 8, 1), new Date(2014, 8, 7)) //=> true
Example 2
// Are 1 September 2014 and 1 September 2015 in the same ISO week? const result = isSameISOWeek(new Date(2014, 8, 1), new Date(2015, 8, 1)) //=> false
function isSameISOWeekYear
isSameISOWeekYear: ( laterDate: DateArg<Date> & {}, earlierDate: DateArg<Date> & {}, options?: IsSameISOWeekYearOptions | undefined) => boolean;
isSameISOWeekYear ISO Week-Numbering Year Helpers Are the given dates in the same ISO week-numbering year?
Are the given dates in the same ISO week-numbering year?
ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_date
Parameter laterDate
The first date to check
Parameter earlierDate
The second date to check
Parameter options
An object with options
Returns
The dates are in the same ISO week-numbering year
Example 1
// Are 29 December 2003 and 2 January 2005 in the same ISO week-numbering year? const result = isSameISOWeekYear(new Date(2003, 11, 29), new Date(2005, 0, 2)) //=> true
function isSameMinute
isSameMinute: ( laterDate: DateArg<Date> & {}, earlierDate: DateArg<Date> & {}) => boolean;
isSameMinute Minute Helpers Are the given dates in the same minute (and hour and day)?
Are the given dates in the same minute (and hour and day)?
Parameter laterDate
The first date to check
Parameter earlierDate
The second date to check
Returns
The dates are in the same minute (and hour and day)
Example 1
// Are 4 September 2014 06:30:00 and 4 September 2014 06:30:15 in the same minute? const result = isSameMinute( new Date(2014, 8, 4, 6, 30), new Date(2014, 8, 4, 6, 30, 15) ) //=> true
Example 2
// Are 4 September 2014 06:30:00 and 5 September 2014 06:30:00 in the same minute? const result = isSameMinute( new Date(2014, 8, 4, 6, 30), new Date(2014, 8, 5, 6, 30) ) //=> false
function isSameMonth
isSameMonth: ( laterDate: DateArg<Date> & {}, earlierDate: DateArg<Date> & {}, options?: IsSameMonthOptions | undefined) => boolean;
isSameMonth Month Helpers Are the given dates in the same month (and year)?
Are the given dates in the same month (and year)?
Parameter laterDate
The first date to check
Parameter earlierDate
The second date to check
Parameter options
An object with options
Returns
The dates are in the same month (and year)
Example 1
// Are 2 September 2014 and 25 September 2014 in the same month? const result = isSameMonth(new Date(2014, 8, 2), new Date(2014, 8, 25)) //=> true
Example 2
// Are 2 September 2014 and 25 September 2015 in the same month? const result = isSameMonth(new Date(2014, 8, 2), new Date(2015, 8, 25)) //=> false
function isSameQuarter
isSameQuarter: ( laterDate: DateArg<Date> & {}, earlierDate: DateArg<Date> & {}, options?: IsSameQuarterOptions | undefined) => boolean;
isSameQuarter Quarter Helpers Are the given dates in the same quarter (and year)?
Are the given dates in the same quarter (and year)?
Parameter laterDate
The first date to check
Parameter earlierDate
The second date to check
Parameter options
An object with options
Returns
The dates are in the same quarter (and year)
Example 1
// Are 1 January 2014 and 8 March 2014 in the same quarter? const result = isSameQuarter(new Date(2014, 0, 1), new Date(2014, 2, 8)) //=> true
Example 2
// Are 1 January 2014 and 1 January 2015 in the same quarter? const result = isSameQuarter(new Date(2014, 0, 1), new Date(2015, 0, 1)) //=> false
function isSameSecond
isSameSecond: ( laterDate: DateArg<Date> & {}, earlierDate: DateArg<Date> & {}) => boolean;
isSameSecond Second Helpers Are the given dates in the same second (and hour and day)?
Are the given dates in the same second (and hour and day)?
Parameter laterDate
The first date to check
Parameter earlierDate
The second date to check
Returns
The dates are in the same second (and hour and day)
Example 1
// Are 4 September 2014 06:30:15.000 and 4 September 2014 06:30.15.500 in the same second? const result = isSameSecond( new Date(2014, 8, 4, 6, 30, 15), new Date(2014, 8, 4, 6, 30, 15, 500) ) //=> true
Example 2
// Are 4 September 2014 06:00:15.000 and 4 September 2014 06:01.15.000 in the same second? const result = isSameSecond( new Date(2014, 8, 4, 6, 0, 15), new Date(2014, 8, 4, 6, 1, 15) ) //=> false
Example 3
// Are 4 September 2014 06:00:15.000 and 5 September 2014 06:00.15.000 in the same second? const result = isSameSecond( new Date(2014, 8, 4, 6, 0, 15), new Date(2014, 8, 5, 6, 0, 15) ) //=> false
function isSameWeek
isSameWeek: ( laterDate: DateArg<Date> & {}, earlierDate: DateArg<Date> & {}, options?: IsSameWeekOptions) => boolean;
isSameWeek Week Helpers Are the given dates in the same week (and month and year)?
Are the given dates in the same week (and month and year)?
Parameter laterDate
The first date to check
Parameter earlierDate
The second date to check
Parameter options
An object with options
Returns
The dates are in the same week (and month and year)
Example 1
// Are 31 August 2014 and 4 September 2014 in the same week? const result = isSameWeek(new Date(2014, 7, 31), new Date(2014, 8, 4)) //=> true
Example 2
// If week starts with Monday, // are 31 August 2014 and 4 September 2014 in the same week? const result = isSameWeek(new Date(2014, 7, 31), new Date(2014, 8, 4), { weekStartsOn: 1 }) //=> false
Example 3
// Are 1 January 2014 and 1 January 2015 in the same week? const result = isSameWeek(new Date(2014, 0, 1), new Date(2015, 0, 1)) //=> false
function isSameYear
isSameYear: ( laterDate: DateArg<Date> & {}, earlierDate: DateArg<Date> & {}, options?: IsSameYearOptions | undefined) => boolean;
isSameYear Year Helpers Are the given dates in the same year?
Are the given dates in the same year?
Parameter laterDate
The first date to check
Parameter earlierDate
The second date to check
Parameter options
An object with options
Returns
The dates are in the same year
Example 1
// Are 2 September 2014 and 25 September 2014 in the same year? const result = isSameYear(new Date(2014, 8, 2), new Date(2014, 8, 25)) //=> true
function isSaturday
isSaturday: ( date: DateArg<Date> & {}, options?: IsSaturdayOptions | undefined) => boolean;
isSaturday Weekday Helpers Is the given date Saturday?
Is the given date Saturday?
Parameter date
The date to check
Parameter options
An object with options
Returns
The date is Saturday
Example 1
// Is 27 September 2014 Saturday? const result = isSaturday(new Date(2014, 8, 27)) //=> true
function isSunday
isSunday: ( date: DateArg<Date> & {}, options?: IsSundayOptions | undefined) => boolean;
isSunday Weekday Helpers Is the given date Sunday?
Is the given date Sunday?
Parameter date
The date to check
Parameter options
The options object
Returns
The date is Sunday
Example 1
// Is 21 September 2014 Sunday? const result = isSunday(new Date(2014, 8, 21)) //=> true
function isThisHour
isThisHour: (date: DateArg<Date> & {}, options?: IsThisHourOptions) => boolean;
isThisHour Hour Helpers Is the given date in the same hour as the current date? false
Is the given date in the same hour as the current date?
Parameter date
The date to check
Parameter options
An object with options
Returns
The date is in this hour
Example 1
// If now is 25 September 2014 18:30:15.500, // is 25 September 2014 18:00:00 in this hour? const result = isThisHour(new Date(2014, 8, 25, 18)) //=> true
function isThisISOWeek
isThisISOWeek: ( date: DateArg<Date> & {}, options?: IsThisISOWeekOptions | undefined) => boolean;
isThisISOWeek ISO Week Helpers Is the given date in the same ISO week as the current date? false
Is the given date in the same ISO week as the current date?
ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_date
Parameter date
The date to check
Parameter options
An object with options
Returns
The date is in this ISO week
Example 1
// If today is 25 September 2014, is 22 September 2014 in this ISO week? const result = isThisISOWeek(new Date(2014, 8, 22)) //=> true
function isThisMinute
isThisMinute: (date: DateArg<Date> & {}) => boolean;
isThisMinute Minute Helpers Is the given date in the same minute as the current date? false
Is the given date in the same minute as the current date?
Parameter date
The date to check
Returns
The date is in this minute
Example 1
// If now is 25 September 2014 18:30:15.500, // is 25 September 2014 18:30:00 in this minute? const result = isThisMinute(new Date(2014, 8, 25, 18, 30)) //=> true
function isThisMonth
isThisMonth: ( date: DateArg<Date> & {}, options?: IsThisMonthOptions | undefined) => boolean;
isThisMonth Month Helpers Is the given date in the same month as the current date? false
Is the given date in the same month as the current date?
Parameter date
The date to check
Parameter options
An object with options
Returns
The date is in this month
Example 1
// If today is 25 September 2014, is 15 September 2014 in this month? const result = isThisMonth(new Date(2014, 8, 15)) //=> true
function isThisQuarter
isThisQuarter: ( date: DateArg<Date> & {}, options?: IsThisQuarterOptions) => boolean;
isThisQuarter Quarter Helpers Is the given date in the same quarter as the current date? false
Is the given date in the same quarter as the current date?
Parameter date
The date to check
Parameter options
An object with options
Returns
The date is in this quarter
Example 1
// If today is 25 September 2014, is 2 July 2014 in this quarter? const result = isThisQuarter(new Date(2014, 6, 2)) //=> true
function isThisSecond
isThisSecond: (date: DateArg<Date> & {}) => boolean;
isThisSecond Second Helpers Is the given date in the same second as the current date? false
Is the given date in the same second as the current date?
Parameter date
The date to check
Returns
The date is in this second
Example 1
// If now is 25 September 2014 18:30:15.500, // is 25 September 2014 18:30:15.000 in this second? const result = isThisSecond(new Date(2014, 8, 25, 18, 30, 15)) //=> true
function isThisWeek
isThisWeek: (date: DateArg<Date> & {}, options?: IsThisWeekOptions) => boolean;
isThisWeek Week Helpers Is the given date in the same week as the current date? false
Is the given date in the same week as the current date?
Parameter date
The date to check
Parameter options
The object with options
Returns
The date is in this week
Example 1
// If today is 25 September 2014, is 21 September 2014 in this week? const result = isThisWeek(new Date(2014, 8, 21)) //=> true
Example 2
// If today is 25 September 2014 and week starts with Monday // is 21 September 2014 in this week? const result = isThisWeek(new Date(2014, 8, 21), { weekStartsOn: 1 }) //=> false
function isThisYear
isThisYear: ( date: DateArg<Date> & {}, options?: IsThisYearOptions | undefined) => boolean;
isThisYear Year Helpers Is the given date in the same year as the current date? false
Is the given date in the same year as the current date?
Parameter date
The date to check
Parameter options
An object with options
Returns
The date is in this year
Example 1
// If today is 25 September 2014, is 2 July 2014 in this year? const result = isThisYear(new Date(2014, 6, 2)) //=> true
function isThursday
isThursday: ( date: DateArg<Date> & {}, options?: IsThursdayOptions | undefined) => boolean;
isThursday Weekday Helpers Is the given date Thursday?
Is the given date Thursday?
Parameter date
The date to check
Parameter options
An object with options
Returns
The date is Thursday
Example 1
// Is 25 September 2014 Thursday? const result = isThursday(new Date(2014, 8, 25)) //=> true
function isToday
isToday: ( date: DateArg<Date> & {}, options?: IsTodayOptions | undefined) => boolean;
isToday Day Helpers Is the given date today? false
Is the given date today?
Parameter date
The date to check
Parameter options
An object with options
Returns
The date is today
Example 1
// If today is 6 October 2014, is 6 October 14:00:00 today? const result = isToday(new Date(2014, 9, 6, 14, 0)) //=> true
function isTomorrow
isTomorrow: ( date: DateArg<Date> & {}, options?: IsTomorrowOptions | undefined) => boolean;
isTomorrow Day Helpers Is the given date tomorrow? false
Is the given date tomorrow?
Parameter date
The date to check
Parameter options
An object with options
Returns
The date is tomorrow
Example 1
// If today is 6 October 2014, is 7 October 14:00:00 tomorrow? const result = isTomorrow(new Date(2014, 9, 7, 14, 0)) //=> true
function isTuesday
isTuesday: ( date: DateArg<Date> & {}, options?: IsTuesdayOptions | undefined) => boolean;
isTuesday Weekday Helpers Is the given date Tuesday?
Is the given date Tuesday?
Parameter date
The date to check
Parameter options
An object with options
Returns
The date is Tuesday
Example 1
// Is 23 September 2014 Tuesday? const result = isTuesday(new Date(2014, 8, 23)) //=> true
function isValid
isValid: (date: unknown) => boolean;
isValid Common Helpers Is the given date valid?
Returns false if argument is Invalid Date and true otherwise. Argument is converted to Date using
toDate
. See [toDate](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
Parameter date
The date to check
Returns
The date is valid
Example 1
// For the valid date: const result = isValid(new Date(2014, 1, 31)) //=> true
Example 2
// For the value, convertible into a date: const result = isValid(1393804800000) //=> true
Example 3
// For the invalid date: const result = isValid(new Date('')) //=> false
function isWednesday
isWednesday: ( date: DateArg<Date> & {}, options?: IsWednesdayOptions | undefined) => boolean;
isWednesday Weekday Helpers Is the given date Wednesday?
Is the given date Wednesday?
Parameter date
The date to check
Parameter options
An object with options
Returns
The date is Wednesday
Example 1
// Is 24 September 2014 Wednesday? const result = isWednesday(new Date(2014, 8, 24)) //=> true
function isWeekend
isWeekend: ( date: DateArg<Date> & {}, options?: IsWeekendOptions | undefined) => boolean;
isWeekend Weekday Helpers Does the given date fall on a weekend?
Does the given date fall on a weekend? A weekend is either Saturday (
6
) or Sunday (0
).Parameter date
The date to check
Parameter options
An object with options
Returns
The date falls on a weekend
Example 1
// Does 5 October 2014 fall on a weekend? const result = isWeekend(new Date(2014, 9, 5)) //=> true
function isWithinInterval
isWithinInterval: ( date: DateArg<Date> & {}, interval: Interval, options?: IsWithinIntervalOptions | undefined) => boolean;
isWithinInterval Interval Helpers Is the given date within the interval?
Is the given date within the interval? (Including start and end.)
Parameter date
The date to check
Parameter interval
The interval to check
Parameter options
An object with options
Returns
The date is within the interval
Example 1
// For the date within the interval: isWithinInterval(new Date(2014, 0, 3), { start: new Date(2014, 0, 1), end: new Date(2014, 0, 7) }) // => true
Example 2
// For the date outside of the interval: isWithinInterval(new Date(2014, 0, 10), { start: new Date(2014, 0, 1), end: new Date(2014, 0, 7) }) // => false
Example 3
// For date equal to the interval start: isWithinInterval(date, { start, end: date }) // => true
Example 4
// For date equal to the interval end: isWithinInterval(date, { start: date, end }) // => true
function isYesterday
isYesterday: ( date: DateArg<Date> & {}, options?: IsYesterdayOptions | undefined) => boolean;
isYesterday Day Helpers Is the given date yesterday? false
Is the given date yesterday?
Parameter date
The date to check
Parameter options
An object with options
Returns
The date is yesterday
Example 1
// If today is 6 October 2014, is 5 October 14:00:00 yesterday? const result = isYesterday(new Date(2014, 9, 5, 14, 0)) //=> true
function lastDayOfDecade
lastDayOfDecade: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, options?: LastDayOfDecadeOptions<ResultDate> | undefined) => ResultDate;
lastDayOfDecade Decade Helpers Return the last day of a decade for the given date.
Return the last day of a decade for the given date.
Parameter date
The original date
Parameter options
The options
Returns
The last day of a decade
Example 1
// The last day of a decade for 21 December 2012 21:12:00: const result = lastDayOfDecade(new Date(2012, 11, 21, 21, 12, 00)) //=> Wed Dec 31 2019 00:00:00
function lastDayOfISOWeek
lastDayOfISOWeek: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, options?: LastDayOfISOWeekOptions<ResultDate> | undefined) => ResultDate;
lastDayOfISOWeek ISO Week Helpers Return the last day of an ISO week for the given date.
Return the last day of an ISO week for the given date. The result will be in the local timezone.
ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_date
Parameter date
The original date
Parameter options
An object with options
Returns
The last day of an ISO week
Example 1
// The last day of an ISO week for 2 September 2014 11:55:00: const result = lastDayOfISOWeek(new Date(2014, 8, 2, 11, 55, 0)) //=> Sun Sep 07 2014 00:00:00
function lastDayOfISOWeekYear
lastDayOfISOWeekYear: < DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, options?: LastDayOfISOWeekYearOptions<ResultDate> | undefined) => ResultDate;
lastDayOfISOWeekYear ISO Week-Numbering Year Helpers Return the last day of an ISO week-numbering year for the given date.
Return the last day of an ISO week-numbering year, which always starts 3 days before the year's first Thursday. The result will be in the local timezone.
ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_date
Parameter date
The original date
Parameter options
An object with options
Returns
The end of an ISO week-numbering year
Example 1
// The last day of an ISO week-numbering year for 2 July 2005: const result = lastDayOfISOWeekYear(new Date(2005, 6, 2)) //=> Sun Jan 01 2006 00:00:00
function lastDayOfMonth
lastDayOfMonth: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, options?: LastDayOfMonthOptions<ResultDate> | undefined) => ResultDate;
lastDayOfMonth Month Helpers Return the last day of a month for the given date.
Return the last day of a month for the given date. The result will be in the local timezone.
Parameter date
The original date
Parameter options
An object with options
Returns
The last day of a month
Example 1
// The last day of a month for 2 September 2014 11:55:00: const result = lastDayOfMonth(new Date(2014, 8, 2, 11, 55, 0)) //=> Tue Sep 30 2014 00:00:00
function lastDayOfQuarter
lastDayOfQuarter: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, options?: LastDayOfQuarterOptions<ResultDate> | undefined) => ResultDate;
lastDayOfQuarter Quarter Helpers Return the last day of a year quarter for the given date.
Return the last day of a year quarter for the given date. The result will be in the local timezone.
Parameter date
The original date
Parameter options
The options
Returns
The last day of a quarter
Example 1
// The last day of a quarter for 2 September 2014 11:55:00: const result = lastDayOfQuarter(new Date(2014, 8, 2, 11, 55, 0)) //=> Tue Sep 30 2014 00:00:00
function lastDayOfWeek
lastDayOfWeek: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, options?: LastDayOfWeekOptions<ResultDate>) => ResultDate;
lastDayOfWeek Week Helpers Return the last day of a week for the given date.
Return the last day of a week for the given date. The result will be in the local timezone unless a context is specified.
Parameter date
The original date
Parameter options
An object with options
Returns
The last day of a week
function lastDayOfYear
lastDayOfYear: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, options?: LastDayOfYearOptions<ResultDate>) => ResultDate;
lastDayOfYear Year Helpers Return the last day of a year for the given date.
Return the last day of a year for the given date. The result will be in the local timezone.
Parameter date
The original date
Parameter options
An object with options
Returns
The last day of a year
Example 1
// The last day of a year for 2 September 2014 11:55:00: const result = lastDayOfYear(new Date(2014, 8, 2, 11, 55, 00)) //=> Wed Dec 31 2014 00:00:00
function lightFormat
lightFormat: (date: DateArg<Date> & {}, formatStr: string) => string;
lightFormat Common Helpers Format the date.
Return the formatted date string in the given format. Unlike
format
,lightFormat
doesn't use locales and outputs date using the most popular tokens.> ⚠️ Please note that the
lightFormat
tokens differ from Moment.js and other libraries. > See: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.mdThe characters wrapped between two single quotes characters (') are escaped. Two single quotes in a row, whether inside or outside a quoted sequence, represent a 'real' single quote.
Format of the string is based on Unicode Technical Standard #35: https://www.unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table
Accepted patterns: | Unit | Pattern | Result examples | |---------------------------------|---------|-----------------------------------| | AM, PM | a..aaa | AM, PM | | | aaaa | a.m., p.m. | | | aaaaa | a, p | | Calendar year | y | 44, 1, 1900, 2017 | | | yy | 44, 01, 00, 17 | | | yyy | 044, 001, 000, 017 | | | yyyy | 0044, 0001, 1900, 2017 | | Month (formatting) | M | 1, 2, ..., 12 | | | MM | 01, 02, ..., 12 | | Day of month | d | 1, 2, ..., 31 | | | dd | 01, 02, ..., 31 | | Hour [1-12] | h | 1, 2, ..., 11, 12 | | | hh | 01, 02, ..., 11, 12 | | Hour [0-23] | H | 0, 1, 2, ..., 23 | | | HH | 00, 01, 02, ..., 23 | | Minute | m | 0, 1, ..., 59 | | | mm | 00, 01, ..., 59 | | Second | s | 0, 1, ..., 59 | | | ss | 00, 01, ..., 59 | | Fraction of second | S | 0, 1, ..., 9 | | | SS | 00, 01, ..., 99 | | | SSS | 000, 001, ..., 999 | | | SSSS | ... |
Parameter date
The original date
Parameter format
The string of tokens
Returns
The formatted date string
Throws
Invalid time value
if the date is invalidThrows
format string contains an unescaped latin alphabet character
Example 1
const result = lightFormat(new Date(2014, 1, 11), 'yyyy-MM-dd') //=> '2014-02-11'
function max
max: <DateType extends Date, ResultDate extends Date = DateType>( dates: DateArg<DateType>[], options?: MaxOptions<ResultDate> | undefined) => ResultDate;
max Common Helpers Return the latest of the given dates.
Return the latest of the given dates.
Parameter dates
The dates to compare
Returns
The latest of the dates
Example 1
// Which of these dates is the latest? const result = max([ new Date(1989, 6, 10), new Date(1987, 1, 11), new Date(1995, 6, 2), new Date(1990, 0, 1) ]) //=> Sun Jul 02 1995 00:00:00
function milliseconds
milliseconds: ({ years, months, weeks, days, hours, minutes, seconds,}: Duration) => number;
milliseconds Millisecond Helpers Returns the number of milliseconds in the specified, years, months, weeks, days, hours, minutes and seconds.
Returns the number of milliseconds in the specified, years, months, weeks, days, hours, minutes and seconds.
One years equals 365.2425 days according to the formula:
> Leap year occurs every 4 years, except for years that are divisible by 100 and not divisible by 400. > 1 mean year = (365+1/4-1/100+1/400) days = 365.2425 days
One month is a year divided by 12.
Parameter duration
The object with years, months, weeks, days, hours, minutes and seconds to be added.
Returns
The milliseconds
Example 1
// 1 year in milliseconds milliseconds({ years: 1 }) //=> 31556952000
// 3 months in milliseconds milliseconds({ months: 3 }) //=> 7889238000
function millisecondsToHours
millisecondsToHours: (milliseconds: number) => number;
millisecondsToHours Conversion Helpers Convert milliseconds to hours.
Convert a number of milliseconds to a full number of hours.
Parameter milliseconds
The number of milliseconds to be converted
Returns
The number of milliseconds converted in hours
Example 1
// Convert 7200000 milliseconds to hours: const result = millisecondsToHours(7200000) //=> 2
Example 2
// It uses floor rounding: const result = millisecondsToHours(7199999) //=> 1
function millisecondsToMinutes
millisecondsToMinutes: (milliseconds: number) => number;
millisecondsToMinutes Conversion Helpers Convert milliseconds to minutes.
Convert a number of milliseconds to a full number of minutes.
Parameter milliseconds
The number of milliseconds to be converted
Returns
The number of milliseconds converted in minutes
Example 1
// Convert 60000 milliseconds to minutes: const result = millisecondsToMinutes(60000) //=> 1
Example 2
// It uses floor rounding: const result = millisecondsToMinutes(119999) //=> 1
function millisecondsToSeconds
millisecondsToSeconds: (milliseconds: number) => number;
millisecondsToSeconds Conversion Helpers Convert milliseconds to seconds.
Convert a number of milliseconds to a full number of seconds.
Parameter milliseconds
The number of milliseconds to be converted
Returns
The number of milliseconds converted in seconds
Example 1
// Convert 1000 milliseconds to seconds: const result = millisecondsToSeconds(1000) //=> 1
Example 2
// It uses floor rounding: const result = millisecondsToSeconds(1999) //=> 1
function min
min: <DateType extends Date, ResultDate extends Date = DateType>( dates: Array<DateArg<DateType>>, options?: MinOptions<ResultDate> | undefined) => ResultDate;
min Common Helpers Returns the earliest of the given dates.
Returns the earliest of the given dates.
Parameter dates
The dates to compare
Returns
The earliest of the dates
Example 1
// Which of these dates is the earliest? const result = min([ new Date(1989, 6, 10), new Date(1987, 1, 11), new Date(1995, 6, 2), new Date(1990, 0, 1) ]) //=> Wed Feb 11 1987 00:00:00
function minutesToHours
minutesToHours: (minutes: number) => number;
minutesToHours Conversion Helpers Convert minutes to hours.
Convert a number of minutes to a full number of hours.
Parameter minutes
The number of minutes to be converted
Returns
The number of minutes converted in hours
Example 1
// Convert 140 minutes to hours: const result = minutesToHours(120) //=> 2
Example 2
// It uses floor rounding: const result = minutesToHours(179) //=> 2
function minutesToMilliseconds
minutesToMilliseconds: (minutes: number) => number;
minutesToMilliseconds Conversion Helpers Convert minutes to milliseconds.
Convert a number of minutes to a full number of milliseconds.
Parameter minutes
The number of minutes to be converted
Returns
The number of minutes converted in milliseconds
Example 1
// Convert 2 minutes to milliseconds const result = minutesToMilliseconds(2) //=> 120000
function minutesToSeconds
minutesToSeconds: (minutes: number) => number;
minutesToSeconds Conversion Helpers Convert minutes to seconds.
Convert a number of minutes to a full number of seconds.
Parameter minutes
The number of minutes to be converted
Returns
The number of minutes converted in seconds
Example 1
// Convert 2 minutes to seconds const result = minutesToSeconds(2) //=> 120
function monthsToQuarters
monthsToQuarters: (months: number) => number;
monthsToQuarters Conversion Helpers Convert number of months to quarters.
Convert a number of months to a full number of quarters.
Parameter months
The number of months to be converted.
Returns
The number of months converted in quarters
Example 1
// Convert 6 months to quarters: const result = monthsToQuarters(6) //=> 2
Example 2
// It uses floor rounding: const result = monthsToQuarters(7) //=> 2
function monthsToYears
monthsToYears: (months: number) => number;
monthsToYears Conversion Helpers Convert number of months to years.
Convert a number of months to a full number of years.
Parameter months
The number of months to be converted
Returns
The number of months converted in years
Example 1
// Convert 36 months to years: const result = monthsToYears(36) //=> 3
// It uses floor rounding: const result = monthsToYears(40) //=> 3
function nextDay
nextDay: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, day: Day, options?: NextDayOptions<ResultDate> | undefined) => ResultDate;
nextDay Weekday Helpers When is the next day of the week? 0-6 the day of the week, 0 represents Sunday.
Parameter date
The date to check
Parameter day
Day of the week
Parameter options
An object with options
Returns
The date is the next day of the week
Example 1
// When is the next Monday after Mar, 20, 2020? const result = nextDay(new Date(2020, 2, 20), 1) //=> Mon Mar 23 2020 00:00:00
Example 2
// When is the next Tuesday after Mar, 21, 2020? const result = nextDay(new Date(2020, 2, 21), 2) //=> Tue Mar 24 2020 00:00:00
function nextFriday
nextFriday: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, options?: NextFridayOptions<ResultDate>) => ResultDate;
nextFriday Weekday Helpers When is the next Friday?
When is the next Friday?
Parameter date
The date to start counting from
Parameter options
An object with options
Returns
The next Friday
Example 1
// When is the next Friday after Mar, 22, 2020? const result = nextFriday(new Date(2020, 2, 22)) //=> Fri Mar 27 2020 00:00:00
function nextMonday
nextMonday: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, options?: NextMondayOptions<ResultDate> | undefined) => ResultDate;
nextMonday Weekday Helpers When is the next Monday?
When is the next Monday?
Parameter date
The date to start counting from
Parameter options
An object with options
Returns
The next Monday
Example 1
// When is the next Monday after Mar, 22, 2020? const result = nextMonday(new Date(2020, 2, 22)) //=> Mon Mar 23 2020 00:00:00
function nextSaturday
nextSaturday: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, options?: NextSaturdayOptions<ResultDate>) => ResultDate;
nextSaturday Weekday Helpers When is the next Saturday?
When is the next Saturday?
Parameter date
The date to start counting from
Parameter options
An object with options
Returns
The next Saturday
Example 1
// When is the next Saturday after Mar, 22, 2020? const result = nextSaturday(new Date(2020, 2, 22)) //=> Sat Mar 28 2020 00:00:00
function nextSunday
nextSunday: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, options?: NextSundayOptions<ResultDate> | undefined) => ResultDate;
nextSunday Weekday Helpers When is the next Sunday?
When is the next Sunday?
Parameter date
The date to start counting from
Parameter options
An object with options
Returns
The next Sunday
Example 1
// When is the next Sunday after March 22, 2020? const result = nextSunday(new Date(2020, 2, 22)) //=> Sun Mar 29 2020 00:00:00
function nextThursday
nextThursday: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, options?: NextThursdayOptions<ResultDate> | undefined) => ResultDate;
nextThursday Weekday Helpers When is the next Thursday?
When is the next Thursday?
Parameter date
The date to start counting from
Parameter options
An object with options
Returns
The next Thursday
Example 1
// When is the next Thursday after Mar, 22, 2020? const result = nextThursday(new Date(2020, 2, 22)) //=> Thur Mar 26 2020 00:00:00
function nextTuesday
nextTuesday: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, options?: NextTuesdayOptions<ResultDate>) => ResultDate;
nextTuesday Weekday Helpers When is the next Tuesday?
When is the next Tuesday?
Parameter date
The date to start counting from
Parameter options
An object with options
Returns
The next Tuesday
Example 1
// When is the next Tuesday after Mar, 22, 2020? const result = nextTuesday(new Date(2020, 2, 22)) //=> Tue Mar 24 2020 00:00:00
function nextWednesday
nextWednesday: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, options?: NextWednesdayOptions<ResultDate> | undefined) => ResultDate;
nextWednesday Weekday Helpers When is the next Wednesday?
When is the next Wednesday?
Parameter date
The date to start counting from
Parameter options
An object with options
Returns
The next Wednesday
Example 1
// When is the next Wednesday after Mar, 22, 2020? const result = nextWednesday(new Date(2020, 2, 22)) //=> Wed Mar 25 2020 00:00:00
function parse
parse: <DateType extends Date, ResultDate extends Date = DateType>( dateStr: string, formatStr: string, referenceDate: DateArg<DateType>, options?: ParseOptions<ResultDate>) => ResultDate;
parse Common Helpers Parse the date.
Return the date parsed from string using the given format string.
> ⚠️ Please note that the
format
tokens differ from Moment.js and other libraries. > See: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.mdThe characters in the format string wrapped between two single quotes characters (') are escaped. Two single quotes in a row, whether inside or outside a quoted sequence, represent a 'real' single quote.
Format of the format string is based on Unicode Technical Standard #35: https://www.unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table with a few additions (see note 5 below the table).
Not all tokens are compatible. Combinations that don't make sense or could lead to bugs are prohibited and will throw
RangeError
. For example usage of 24-hour format token with AM/PM token will throw an exception:parse('23 AM', 'HH a', new Date())//=> RangeError: The format string mustn't contain `HH` and `a` at the same timeSee the compatibility table: https://docs.google.com/spreadsheets/d/e/2PACX-1vQOPU3xUhplll6dyoMmVUXHKl_8CRDs6_ueLmex3SoqwhuolkuN3O05l4rqx5h1dKX8eb46Ul-CCSrq/pubhtml?gid=0&single=true
Accepted format string patterns: | Unit |Prior| Pattern | Result examples | Notes | |---------------------------------|-----|---------|-----------------------------------|-------| | Era | 140 | G..GGG | AD, BC | | | | | GGGG | Anno Domini, Before Christ | 2 | | | | GGGGG | A, B | | | Calendar year | 130 | y | 44, 1, 1900, 2017, 9999 | 4 | | | | yo | 44th, 1st, 1900th, 9999999th | 4,5 | | | | yy | 44, 01, 00, 17 | 4 | | | | yyy | 044, 001, 123, 999 | 4 | | | | yyyy | 0044, 0001, 1900, 2017 | 4 | | | | yyyyy | ... | 2,4 | | Local week-numbering year | 130 | Y | 44, 1, 1900, 2017, 9000 | 4 | | | | Yo | 44th, 1st, 1900th, 9999999th | 4,5 | | | | YY | 44, 01, 00, 17 | 4,6 | | | | YYY | 044, 001, 123, 999 | 4 | | | | YYYY | 0044, 0001, 1900, 2017 | 4,6 | | | | YYYYY | ... | 2,4 | | ISO week-numbering year | 130 | R | -43, 1, 1900, 2017, 9999, -9999 | 4,5 | | | | RR | -43, 01, 00, 17 | 4,5 | | | | RRR | -043, 001, 123, 999, -999 | 4,5 | | | | RRRR | -0043, 0001, 2017, 9999, -9999 | 4,5 | | | | RRRRR | ... | 2,4,5 | | Extended year | 130 | u | -43, 1, 1900, 2017, 9999, -999 | 4 | | | | uu | -43, 01, 99, -99 | 4 | | | | uuu | -043, 001, 123, 999, -999 | 4 | | | | uuuu | -0043, 0001, 2017, 9999, -9999 | 4 | | | | uuuuu | ... | 2,4 | | Quarter (formatting) | 120 | Q | 1, 2, 3, 4 | | | | | Qo | 1st, 2nd, 3rd, 4th | 5 | | | | QQ | 01, 02, 03, 04 | | | | | QQQ | Q1, Q2, Q3, Q4 | | | | | QQQQ | 1st quarter, 2nd quarter, ... | 2 | | | | QQQQQ | 1, 2, 3, 4 | 4 | | Quarter (stand-alone) | 120 | q | 1, 2, 3, 4 | | | | | qo | 1st, 2nd, 3rd, 4th | 5 | | | | qq | 01, 02, 03, 04 | | | | | qqq | Q1, Q2, Q3, Q4 | | | | | qqqq | 1st quarter, 2nd quarter, ... | 2 | | | | qqqqq | 1, 2, 3, 4 | 3 | | Month (formatting) | 110 | M | 1, 2, ..., 12 | | | | | Mo | 1st, 2nd, ..., 12th | 5 | | | | MM | 01, 02, ..., 12 | | | | | MMM | Jan, Feb, ..., Dec | | | | | MMMM | January, February, ..., December | 2 | | | | MMMMM | J, F, ..., D | | | Month (stand-alone) | 110 | L | 1, 2, ..., 12 | | | | | Lo | 1st, 2nd, ..., 12th | 5 | | | | LL | 01, 02, ..., 12 | | | | | LLL | Jan, Feb, ..., Dec | | | | | LLLL | January, February, ..., December | 2 | | | | LLLLL | J, F, ..., D | | | Local week of year | 100 | w | 1, 2, ..., 53 | | | | | wo | 1st, 2nd, ..., 53th | 5 | | | | ww | 01, 02, ..., 53 | | | ISO week of year | 100 | I | 1, 2, ..., 53 | 5 | | | | Io | 1st, 2nd, ..., 53th | 5 | | | | II | 01, 02, ..., 53 | 5 | | Day of month | 90 | d | 1, 2, ..., 31 | | | | | do | 1st, 2nd, ..., 31st | 5 | | | | dd | 01, 02, ..., 31 | | | Day of year | 90 | D | 1, 2, ..., 365, 366 | 7 | | | | Do | 1st, 2nd, ..., 365th, 366th | 5 | | | | DD | 01, 02, ..., 365, 366 | 7 | | | | DDD | 001, 002, ..., 365, 366 | | | | | DDDD | ... | 2 | | Day of week (formatting) | 90 | E..EEE | Mon, Tue, Wed, ..., Sun | | | | | EEEE | Monday, Tuesday, ..., Sunday | 2 | | | | EEEEE | M, T, W, T, F, S, S | | | | | EEEEEE | Mo, Tu, We, Th, Fr, Sa, Su | | | ISO day of week (formatting) | 90 | i | 1, 2, 3, ..., 7 | 5 | | | | io | 1st, 2nd, ..., 7th | 5 | | | | ii | 01, 02, ..., 07 | 5 | | | | iii | Mon, Tue, Wed, ..., Sun | 5 | | | | iiii | Monday, Tuesday, ..., Sunday | 2,5 | | | | iiiii | M, T, W, T, F, S, S | 5 | | | | iiiiii | Mo, Tu, We, Th, Fr, Sa, Su | 5 | | Local day of week (formatting) | 90 | e | 2, 3, 4, ..., 1 | | | | | eo | 2nd, 3rd, ..., 1st | 5 | | | | ee | 02, 03, ..., 01 | | | | | eee | Mon, Tue, Wed, ..., Sun | | | | | eeee | Monday, Tuesday, ..., Sunday | 2 | | | | eeeee | M, T, W, T, F, S, S | | | | | eeeeee | Mo, Tu, We, Th, Fr, Sa, Su | | | Local day of week (stand-alone) | 90 | c | 2, 3, 4, ..., 1 | | | | | co | 2nd, 3rd, ..., 1st | 5 | | | | cc | 02, 03, ..., 01 | | | | | ccc | Mon, Tue, Wed, ..., Sun | | | | | cccc | Monday, Tuesday, ..., Sunday | 2 | | | | ccccc | M, T, W, T, F, S, S | | | | | cccccc | Mo, Tu, We, Th, Fr, Sa, Su | | | AM, PM | 80 | a..aaa | AM, PM | | | | | aaaa | a.m., p.m. | 2 | | | | aaaaa | a, p | | | AM, PM, noon, midnight | 80 | b..bbb | AM, PM, noon, midnight | | | | | bbbb | a.m., p.m., noon, midnight | 2 | | | | bbbbb | a, p, n, mi | | | Flexible day period | 80 | B..BBB | at night, in the morning, ... | | | | | BBBB | at night, in the morning, ... | 2 | | | | BBBBB | at night, in the morning, ... | | | Hour [1-12] | 70 | h | 1, 2, ..., 11, 12 | | | | | ho | 1st, 2nd, ..., 11th, 12th | 5 | | | | hh | 01, 02, ..., 11, 12 | | | Hour [0-23] | 70 | H | 0, 1, 2, ..., 23 | | | | | Ho | 0th, 1st, 2nd, ..., 23rd | 5 | | | | HH | 00, 01, 02, ..., 23 | | | Hour [0-11] | 70 | K | 1, 2, ..., 11, 0 | | | | | Ko | 1st, 2nd, ..., 11th, 0th | 5 | | | | KK | 01, 02, ..., 11, 00 | | | Hour [1-24] | 70 | k | 24, 1, 2, ..., 23 | | | | | ko | 24th, 1st, 2nd, ..., 23rd | 5 | | | | kk | 24, 01, 02, ..., 23 | | | Minute | 60 | m | 0, 1, ..., 59 | | | | | mo | 0th, 1st, ..., 59th | 5 | | | | mm | 00, 01, ..., 59 | | | Second | 50 | s | 0, 1, ..., 59 | | | | | so | 0th, 1st, ..., 59th | 5 | | | | ss | 00, 01, ..., 59 | | | Seconds timestamp | 40 | t | 512969520 | | | | | tt | ... | 2 | | Fraction of second | 30 | S | 0, 1, ..., 9 | | | | | SS | 00, 01, ..., 99 | | | | | SSS | 000, 001, ..., 999 | | | | | SSSS | ... | 2 | | Milliseconds timestamp | 20 | T | 512969520900 | | | | | TT | ... | 2 | | Timezone (ISO-8601 w/ Z) | 10 | X | -08, +0530, Z | | | | | XX | -0800, +0530, Z | | | | | XXX | -08:00, +05:30, Z | | | | | XXXX | -0800, +0530, Z, +123456 | 2 | | | | XXXXX | -08:00, +05:30, Z, +12:34:56 | | | Timezone (ISO-8601 w/o Z) | 10 | x | -08, +0530, +00 | | | | | xx | -0800, +0530, +0000 | | | | | xxx | -08:00, +05:30, +00:00 | 2 | | | | xxxx | -0800, +0530, +0000, +123456 | | | | | xxxxx | -08:00, +05:30, +00:00, +12:34:56 | | | Long localized date | NA | P | 05/29/1453 | 5,8 | | | | PP | May 29, 1453 | | | | | PPP | May 29th, 1453 | | | | | PPPP | Sunday, May 29th, 1453 | 2,5,8 | | Long localized time | NA | p | 12:00 AM | 5,8 | | | | pp | 12:00:00 AM | | | Combination of date and time | NA | Pp | 05/29/1453, 12:00 AM | | | | | PPpp | May 29, 1453, 12:00:00 AM | | | | | PPPpp | May 29th, 1453 at ... | | | | | PPPPpp | Sunday, May 29th, 1453 at ... | 2,5,8 | Notes: 1. "Formatting" units (e.g. formatting quarter) in the default en-US locale are the same as "stand-alone" units, but are different in some languages. "Formatting" units are declined according to the rules of the language in the context of a date. "Stand-alone" units are always nominative singular. In
format
function, they will produce different result:format(new Date(2017, 10, 6), 'do LLLL', {locale: cs}) //=> '6. listopad'
format(new Date(2017, 10, 6), 'do MMMM', {locale: cs}) //=> '6. listopadu'
parse
will try to match both formatting and stand-alone units interchangeably.2. Any sequence of the identical letters is a pattern, unless it is escaped by the single quote characters (see below). If the sequence is longer than listed in table: - for numerical units (
yyyyyyyy
)parse
will try to match a number as wide as the sequence - for text units (MMMMMMMM
)parse
will try to match the widest variation of the unit. These variations are marked with "2" in the last column of the table.3.
QQQQQ
andqqqqq
could be not strictly numerical in some locales. These tokens represent the shortest form of the quarter.4. The main difference between
y
andu
patterns are B.C. years:| Year |
y
|u
| |------|-----|-----| | AC 1 | 1 | 1 | | BC 1 | 1 | 0 | | BC 2 | 2 | -1 |Also
yy
will try to guess the century of two digit year by proximity withreferenceDate
:parse('50', 'yy', new Date(2018, 0, 1)) //=> Sat Jan 01 2050 00:00:00
parse('75', 'yy', new Date(2018, 0, 1)) //=> Wed Jan 01 1975 00:00:00
while
uu
will just assign the year as is:parse('50', 'uu', new Date(2018, 0, 1)) //=> Sat Jan 01 0050 00:00:00
parse('75', 'uu', new Date(2018, 0, 1)) //=> Tue Jan 01 0075 00:00:00
The same difference is true for local and ISO week-numbering years (
Y
andR
), except local week-numbering years are dependent onoptions.weekStartsOn
andoptions.firstWeekContainsDate
(compare [setISOWeekYear](https://date-fns.org/docs/setISOWeekYear) and [setWeekYear](https://date-fns.org/docs/setWeekYear)).5. These patterns are not in the Unicode Technical Standard #35: -
i
: ISO day of week -I
: ISO week of year -R
: ISO week-numbering year -o
: ordinal number modifier -P
: long localized date -p
: long localized time6.
YY
andYYYY
tokens represent week-numbering years but they are often confused with years. You should enableoptions.useAdditionalWeekYearTokens
to use them. See: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md7.
D
andDD
tokens represent days of the year but they are often confused with days of the month. You should enableoptions.useAdditionalDayOfYearTokens
to use them. See: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md8.
P+
tokens do not have a defined priority since they are merely aliases to other tokens based on the given locale.using
en-US
locale:P
=>MM/dd/yyyy
usingen-US
locale:p
=>hh:mm a
usingpt-BR
locale:P
=>dd/MM/yyyy
usingpt-BR
locale:p
=>HH:mm
Values will be assigned to the date in the descending order of its unit's priority. Units of an equal priority overwrite each other in the order of appearance.
If no values of higher priority are parsed (e.g. when parsing string 'January 1st' without a year), the values will be taken from 3rd argument
referenceDate
which works as a context of parsing.referenceDate
must be passed for correct work of the function. If you're not sure whichreferenceDate
to supply, create a new instance of Date:parse('02/11/2014', 'MM/dd/yyyy', new Date())
In this case parsing will be done in the context of the current date. IfreferenceDate
isInvalid Date
or a value not convertible to validDate
, thenInvalid Date
will be returned.The result may vary by locale.
If
formatString
matches withdateString
but does not provides tokens,referenceDate
will be returned.If parsing failed,
Invalid Date
will be returned. Invalid Date is a Date, whose time value is NaN. Time value of Date: http://es5.github.io/#x15.9.1.1Parameter dateStr
The string to parse
Parameter formatStr
The string of tokens
Parameter referenceDate
defines values missing from the parsed dateString
Parameter options
An object with options. see: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md see: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md
Returns
The parsed date
Throws
options.locale
must containmatch
propertyThrows
use
yyyy
instead ofYYYY
for formatting years using [format provided] to the input [input provided]; see: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.mdThrows
use
yy
instead ofYY
for formatting years using [format provided] to the input [input provided]; see: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.mdThrows
use
d
instead ofD
for formatting days of the month using [format provided] to the input [input provided]; see: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.mdThrows
use
dd
instead ofDD
for formatting days of the month using [format provided] to the input [input provided]; see: https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.mdThrows
format string contains an unescaped latin alphabet character
Example 1
// Parse 11 February 2014 from middle-endian format: var result = parse('02/11/2014', 'MM/dd/yyyy', new Date()) //=> Tue Feb 11 2014 00:00:00
Example 2
// Parse 28th of February in Esperanto locale in the context of 2010 year: import eo from 'date-fns/locale/eo' var result = parse('28-a de februaro', "do 'de' MMMM", new Date(2010, 0, 1), { locale: eo }) //=> Sun Feb 28 2010 00:00:00
function parseISO
parseISO: <DateType extends Date, ResultDate extends Date = DateType>( argument: string, options?: ParseISOOptions<ResultDate>) => ResultDate;
parseISO Common Helpers Parse ISO string
Parse the given string in ISO 8601 format and return an instance of Date.
Function accepts complete ISO 8601 formats as well as partial implementations. ISO 8601: http://en.wikipedia.org/wiki/ISO_8601
If the argument isn't a string, the function cannot parse the string or the values are invalid, it returns Invalid Date.
Parameter argument
The value to convert
Parameter options
An object with options
Returns
The parsed date in the local time zone
Example 1
// Convert string '2014-02-11T11:30:30' to date: const result = parseISO('2014-02-11T11:30:30') //=> Tue Feb 11 2014 11:30:30
Example 2
// Convert string '+02014101' to date, // if the additional number of digits in the extended year format is 1: const result = parseISO('+02014101', { additionalDigits: 1 }) //=> Fri Apr 11 2014 00:00:00
function parseJSON
parseJSON: <ResultDate extends Date = Date>( dateStr: string, options?: ParseJSONOptions<ResultDate> | undefined) => ResultDate;
Converts a complete ISO date string in UTC time, the typical format for transmitting a date in JSON, to a JavaScript
Date
instance.This is a minimal implementation for converting dates retrieved from a JSON API to a
Date
instance which can be used with other functions in thedate-fns
library. The following formats are supported:-
2000-03-15T05:20:10.123Z
: The output of.toISOString()
andJSON.stringify(new Date())
-2000-03-15T05:20:10Z
: Without milliseconds -2000-03-15T05:20:10+00:00
: With a zero offset, the default JSON encoded format in some other languages -2000-03-15T05:20:10+05:45
: With a positive or negative offset, the default JSON encoded format in some other languages -2000-03-15T05:20:10+0000
: With a zero offset without a colon -2000-03-15T05:20:10
: Without a trailing 'Z' symbol -2000-03-15T05:20:10.1234567
: Up to 7 digits in milliseconds field. Only first 3 are taken into account since JS does not allow fractional milliseconds -2000-03-15 05:20:10
: With a space instead of a 'T' separator for APIs returning a SQL date without reformattingFor convenience and ease of use these other input types are also supported via [toDate](https://date-fns.org/docs/toDate):
- A
Date
instance will be cloned - Anumber
will be treated as a timestampAny other input type or invalid date strings will return an
Invalid Date
.Parameter dateStr
A fully formed ISO8601 date string to convert
Parameter options
An object with options
Returns
The parsed date in the local time zone
function previousDay
previousDay: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, day: Day, options?: PreviousDayOptions<ResultDate> | undefined) => ResultDate;
previousDay Weekday Helpers When is the previous day of the week?
When is the previous day of the week? 0-6 the day of the week, 0 represents Sunday.
Parameter date
The date to check
Parameter day
The day of the week
Parameter options
An object with options
Returns
The date is the previous day of week
Example 1
// When is the previous Monday before Mar, 20, 2020? const result = previousDay(new Date(2020, 2, 20), 1) //=> Mon Mar 16 2020 00:00:00
Example 2
// When is the previous Tuesday before Mar, 21, 2020? const result = previousDay(new Date(2020, 2, 21), 2) //=> Tue Mar 17 2020 00:00:00
function previousFriday
previousFriday: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, options?: PreviousFridayOptions<ResultDate> | undefined) => ResultDate;
previousFriday Weekday Helpers When is the previous Friday?
When is the previous Friday?
Parameter date
The date to start counting from
Parameter options
The options
Returns
The previous Friday
Example 1
// When is the previous Friday before Jun, 19, 2021? const result = previousFriday(new Date(2021, 5, 19)) //=> Fri June 18 2021 00:00:00
function previousMonday
previousMonday: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, options?: PreviousMondayOptions<ResultDate> | undefined) => ResultDate;
previousMonday Weekday Helpers When is the previous Monday?
When is the previous Monday?
Parameter date
The date to start counting from
Parameter options
An object with options
Returns
The previous Monday
Example 1
// When is the previous Monday before Jun, 18, 2021? const result = previousMonday(new Date(2021, 5, 18)) //=> Mon June 14 2021 00:00:00
function previousSaturday
previousSaturday: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, options?: PreviousSaturdayOptions<ResultDate> | undefined) => ResultDate;
previousSaturday Weekday Helpers When is the previous Saturday?
When is the previous Saturday?
Parameter date
The date to start counting from
Parameter options
The options
Returns
The previous Saturday
Example 1
// When is the previous Saturday before Jun, 20, 2021? const result = previousSaturday(new Date(2021, 5, 20)) //=> Sat June 19 2021 00:00:00
function previousSunday
previousSunday: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, options?: PreviousSundayOptions<ResultDate> | undefined) => ResultDate;
previousSunday Weekday Helpers When is the previous Sunday?
When is the previous Sunday?
Parameter date
The date to start counting from
Parameter options
The options
Returns
The previous Sunday
Example 1
// When is the previous Sunday before Jun, 21, 2021? const result = previousSunday(new Date(2021, 5, 21)) //=> Sun June 20 2021 00:00:00
function previousThursday
previousThursday: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, options?: PreviousThursdayOptions<ResultDate> | undefined) => ResultDate;
previousThursday Weekday Helpers When is the previous Thursday?
When is the previous Thursday?
Parameter date
The date to start counting from
Parameter options
An object with options
Returns
The previous Thursday
Example 1
// When is the previous Thursday before Jun, 18, 2021? const result = previousThursday(new Date(2021, 5, 18)) //=> Thu June 17 2021 00:00:00
function previousTuesday
previousTuesday: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, options?: PreviousTuesdayOptions<ResultDate> | undefined) => ResultDate;
previousTuesday Weekday Helpers When is the previous Tuesday?
When is the previous Tuesday?
Parameter date
The date to start counting from
Parameter options
An object with options
Returns
The previous Tuesday
Example 1
// When is the previous Tuesday before Jun, 18, 2021? const result = previousTuesday(new Date(2021, 5, 18)) //=> Tue June 15 2021 00:00:00
function previousWednesday
previousWednesday: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, options?: PreviousWednesdayOptions<ResultDate> | undefined) => ResultDate;
previousWednesday Weekday Helpers When is the previous Wednesday?
When is the previous Wednesday?
Parameter date
The date to start counting from
Parameter options
An object with options
Returns
The previous Wednesday
Example 1
// When is the previous Wednesday before Jun, 18, 2021? const result = previousWednesday(new Date(2021, 5, 18)) //=> Wed June 16 2021 00:00:00
function quartersToMonths
quartersToMonths: (quarters: number) => number;
quartersToMonths Conversion Helpers Convert number of quarters to months.
Convert a number of quarters to a full number of months.
Parameter quarters
The number of quarters to be converted
Returns
The number of quarters converted in months
Example 1
// Convert 2 quarters to months const result = quartersToMonths(2) //=> 6
function quartersToYears
quartersToYears: (quarters: number) => number;
quartersToYears Conversion Helpers Convert number of quarters to years.
Convert a number of quarters to a full number of years.
Parameter quarters
The number of quarters to be converted
Returns
The number of quarters converted in years
Example 1
// Convert 8 quarters to years const result = quartersToYears(8) //=> 2
Example 2
// It uses floor rounding: const result = quartersToYears(11) //=> 2
function roundToNearestHours
roundToNearestHours: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, options?: RoundToNearestHoursOptions<ResultDate>) => ResultDate;
roundToNearestHours Hour Helpers Rounds the given date to the nearest hour
Rounds the given date to the nearest hour (or number of hours). Rounds up when the given date is exactly between the nearest round hours.
Parameter date
The date to round
Parameter options
An object with options.
Returns
The new date rounded to the closest hour
Example 1
// Round 10 July 2014 12:34:56 to nearest hour: const result = roundToNearestHours(new Date(2014, 6, 10, 12, 34, 56)) //=> Thu Jul 10 2014 13:00:00
Example 2
// Round 10 July 2014 12:34:56 to nearest half hour: const result = roundToNearestHours(new Date(2014, 6, 10, 12, 34, 56), { nearestTo: 6 }) //=> Thu Jul 10 2014 12:00:00
Example 3
// Round 10 July 2014 12:34:56 to nearest half hour: const result = roundToNearestHours(new Date(2014, 6, 10, 12, 34, 56), { nearestTo: 8 }) //=> Thu Jul 10 2014 16:00:00
Example 4
// Floor (rounds down) 10 July 2014 12:34:56 to nearest hour: const result = roundToNearestHours(new Date(2014, 6, 10, 1, 23, 45), { roundingMethod: 'ceil' }) //=> Thu Jul 10 2014 02:00:00
Example 5
// Ceil (rounds up) 10 July 2014 12:34:56 to nearest quarter hour: const result = roundToNearestHours(new Date(2014, 6, 10, 12, 34, 56), { roundingMethod: 'floor', nearestTo: 8 }) //=> Thu Jul 10 2014 08:00:00
function roundToNearestMinutes
roundToNearestMinutes: < DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, options?: RoundToNearestMinutesOptions<ResultDate>) => ResultDate;
roundToNearestMinutes Minute Helpers Rounds the given date to the nearest minute
Rounds the given date to the nearest minute (or number of minutes). Rounds up when the given date is exactly between the nearest round minutes.
Parameter date
The date to round
Parameter options
An object with options.
Returns
The new date rounded to the closest minute
Example 1
// Round 10 July 2014 12:12:34 to nearest minute: const result = roundToNearestMinutes(new Date(2014, 6, 10, 12, 12, 34)) //=> Thu Jul 10 2014 12:13:00
Example 2
// Round 10 July 2014 12:12:34 to nearest quarter hour: const result = roundToNearestMinutes(new Date(2014, 6, 10, 12, 12, 34), { nearestTo: 15 }) //=> Thu Jul 10 2014 12:15:00
Example 3
// Floor (rounds down) 10 July 2014 12:12:34 to nearest minute: const result = roundToNearestMinutes(new Date(2014, 6, 10, 12, 12, 34), { roundingMethod: 'floor' }) //=> Thu Jul 10 2014 12:12:00
Example 4
// Ceil (rounds up) 10 July 2014 12:12:34 to nearest half hour: const result = roundToNearestMinutes(new Date(2014, 6, 10, 12, 12, 34), { roundingMethod: 'ceil', nearestTo: 30 }) //=> Thu Jul 10 2014 12:30:00
function secondsToHours
secondsToHours: (seconds: number) => number;
secondsToHours Conversion Helpers Convert seconds to hours.
Convert a number of seconds to a full number of hours.
Parameter seconds
The number of seconds to be converted
Returns
The number of seconds converted in hours
Example 1
// Convert 7200 seconds into hours const result = secondsToHours(7200) //=> 2
Example 2
// It uses floor rounding: const result = secondsToHours(7199) //=> 1
function secondsToMilliseconds
secondsToMilliseconds: (seconds: number) => number;
secondsToMilliseconds Conversion Helpers Convert seconds to milliseconds.
Convert a number of seconds to a full number of milliseconds.
Parameter seconds
The number of seconds to be converted
Returns
The number of seconds converted in milliseconds
Example 1
// Convert 2 seconds into milliseconds const result = secondsToMilliseconds(2) //=> 2000
function secondsToMinutes
secondsToMinutes: (seconds: number) => number;
secondsToMinutes Conversion Helpers Convert seconds to minutes.
Convert a number of seconds to a full number of minutes.
Parameter seconds
The number of seconds to be converted
Returns
The number of seconds converted in minutes
Example 1
// Convert 120 seconds into minutes const result = secondsToMinutes(120) //=> 2
Example 2
// It uses floor rounding: const result = secondsToMinutes(119) //=> 1
function set
set: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, values: DateValues, options?: SetOptions<ResultDate>) => ResultDate;
set Common Helpers Set date values to a given date.
Set date values to a given date.
Sets time values to date from object
values
. A value is not set if it is undefined or null or doesn't exist invalues
.Note about bundle size:
set
does not internally usesetX
functions from date-fns but instead opts to use nativeDate#setX
methods. If you use this function, you may not want to include the othersetX
functions that date-fns provides if you are concerned about the bundle size.Parameter date
The date to be changed
Parameter values
The date values to be set
Parameter options
The options
Returns
The new date with options set
Example 1
// Transform 1 September 2014 into 20 October 2015 in a single line: const result = set(new Date(2014, 8, 20), { year: 2015, month: 9, date: 20 }) //=> Tue Oct 20 2015 00:00:00
Example 2
// Set 12 PM to 1 September 2014 01:23:45 to 1 September 2014 12:00:00: const result = set(new Date(2014, 8, 1, 1, 23, 45), { hours: 12 }) //=> Mon Sep 01 2014 12:23:45
function setDate
setDate: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, dayOfMonth: number, options?: SetDateOptions<ResultDate> | undefined) => ResultDate;
setDate Day Helpers Set the day of the month to the given date.
Set the day of the month to the given date.
Parameter date
The date to be changed
Parameter dayOfMonth
The day of the month of the new date
Parameter options
The options
Returns
The new date with the day of the month set
Example 1
// Set the 30th day of the month to 1 September 2014: const result = setDate(new Date(2014, 8, 1), 30) //=> Tue Sep 30 2014 00:00:00
function setDay
setDay: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, day: number, options?: SetDayOptions<ResultDate>) => ResultDate;
setDay Weekday Helpers Set the day of the week to the given date.
Set the day of the week to the given date.
Parameter date
The date to be changed
Parameter day
The day of the week of the new date
Parameter options
An object with options.
Returns
The new date with the day of the week set
Example 1
// Set week day to Sunday, with the default weekStartsOn of Sunday: const result = setDay(new Date(2014, 8, 1), 0) //=> Sun Aug 31 2014 00:00:00
Example 2
// Set week day to Sunday, with a weekStartsOn of Monday: const result = setDay(new Date(2014, 8, 1), 0, { weekStartsOn: 1 }) //=> Sun Sep 07 2014 00:00:00
function setDayOfYear
setDayOfYear: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, dayOfYear: number, options?: SetDayOfYearOptions<ResultDate> | undefined) => ResultDate;
setDayOfYear Day Helpers Set the day of the year to the given date.
Set the day of the year to the given date.
Parameter date
The date to be changed
Parameter dayOfYear
The day of the year of the new date
Parameter options
An object with options
Returns
The new date with the day of the year set
Example 1
// Set the 2nd day of the year to 2 July 2014: const result = setDayOfYear(new Date(2014, 6, 2), 2) //=> Thu Jan 02 2014 00:00:00
function setDefaultOptions
setDefaultOptions: (options: DefaultOptions) => void;
setDefaultOptions Common Helpers Set default options including locale. false
Sets the defaults for
options.locale
,options.weekStartsOn
andoptions.firstWeekContainsDate
arguments for all functions.Parameter options
An object with options
Example 1
// Set global locale: import { es } from 'date-fns/locale' setDefaultOptions({ locale: es }) const result = format(new Date(2014, 8, 2), 'PPPP') //=> 'martes, 2 de septiembre de 2014'
Example 2
// Start of the week for 2 September 2014: const result = startOfWeek(new Date(2014, 8, 2)) //=> Sun Aug 31 2014 00:00:00
Example 3
// Start of the week for 2 September 2014, // when we set that week starts on Monday by default: setDefaultOptions({ weekStartsOn: 1 }) const result = startOfWeek(new Date(2014, 8, 2)) //=> Mon Sep 01 2014 00:00:00
Example 4
// Manually set options take priority over default options: setDefaultOptions({ weekStartsOn: 1 }) const result = startOfWeek(new Date(2014, 8, 2), { weekStartsOn: 0 }) //=> Sun Aug 31 2014 00:00:00
Example 5
// Remove the option by setting it to
undefined
: setDefaultOptions({ weekStartsOn: 1 }) setDefaultOptions({ weekStartsOn: undefined }) const result = startOfWeek(new Date(2014, 8, 2)) //=> Sun Aug 31 2014 00:00:00
function setHours
setHours: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, hours: number, options?: SetHoursOptions<ResultDate> | undefined) => ResultDate;
setHours Hour Helpers Set the hours to the given date.
Set the hours to the given date.
Parameter date
The date to be changed
Parameter hours
The hours of the new date
Parameter options
An object with options
Returns
The new date with the hours set
Example 1
// Set 4 hours to 1 September 2014 11:30:00: const result = setHours(new Date(2014, 8, 1, 11, 30), 4) //=> Mon Sep 01 2014 04:30:00
function setISODay
setISODay: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, day: number, options?: SetISODayOptions<ResultDate> | undefined) => ResultDate;
setISODay Weekday Helpers Set the day of the ISO week to the given date.
Set the day of the ISO week to the given date. ISO week starts with Monday. 7 is the index of Sunday, 1 is the index of Monday, etc.
Parameter date
The date to be changed
Parameter day
The day of the ISO week of the new date
Parameter options
An object with options
Returns
The new date with the day of the ISO week set
Example 1
// Set Sunday to 1 September 2014: const result = setISODay(new Date(2014, 8, 1), 7) //=> Sun Sep 07 2014 00:00:00
function setISOWeek
setISOWeek: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, week: number, options?: SetISOWeekOptions<ResultDate>) => ResultDate;
setISOWeek ISO Week Helpers Set the ISO week to the given date.
Set the ISO week to the given date, saving the weekday number.
ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_date
Parameter date
The date to be changed
Parameter week
The ISO week of the new date
Parameter options
An object with options
Returns
The new date with the ISO week set
Example 1
// Set the 53rd ISO week to 7 August 2004: const result = setISOWeek(new Date(2004, 7, 7), 53) //=> Sat Jan 01 2005 00:00:00
function setISOWeekYear
setISOWeekYear: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, weekYear: number, options?: SetISOWeekYearOptions<ResultDate> | undefined) => ResultDate;
setISOWeekYear ISO Week-Numbering Year Helpers Set the ISO week-numbering year to the given date.
Set the ISO week-numbering year to the given date, saving the week number and the weekday number.
ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_date
Parameter date
The date to be changed
Parameter weekYear
The ISO week-numbering year of the new date
Parameter options
An object with options
Returns
The new date with the ISO week-numbering year set
Example 1
// Set ISO week-numbering year 2007 to 29 December 2008: const result = setISOWeekYear(new Date(2008, 11, 29), 2007) //=> Mon Jan 01 2007 00:00:00
function setMilliseconds
setMilliseconds: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, milliseconds: number, options?: SetMillisecondsOptions<ResultDate> | undefined) => ResultDate;
setMilliseconds Millisecond Helpers Set the milliseconds to the given date.
Set the milliseconds to the given date.
Parameter date
The date to be changed
Parameter milliseconds
The milliseconds of the new date
Parameter options
The options
Returns
The new date with the milliseconds set
Example 1
// Set 300 milliseconds to 1 September 2014 11:30:40.500: const result = setMilliseconds(new Date(2014, 8, 1, 11, 30, 40, 500), 300) //=> Mon Sep 01 2014 11:30:40.300
function setMinutes
setMinutes: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, minutes: number, options?: SetMinutesOptions<ResultDate> | undefined) => ResultDate;
setMinutes Minute Helpers Set the minutes to the given date.
Set the minutes to the given date.
Parameter date
The date to be changed
Parameter minutes
The minutes of the new date
Parameter options
An object with options
Returns
The new date with the minutes set
Example 1
// Set 45 minutes to 1 September 2014 11:30:40: const result = setMinutes(new Date(2014, 8, 1, 11, 30, 40), 45) //=> Mon Sep 01 2014 11:45:40
function setMonth
setMonth: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, month: number, options?: SetMonthOptions<ResultDate> | undefined) => ResultDate;
setMonth Month Helpers Set the month to the given date.
Set the month to the given date.
Parameter date
The date to be changed
Parameter month
The month index to set (0-11)
Parameter options
The options
Returns
The new date with the month set
Example 1
// Set February to 1 September 2014: const result = setMonth(new Date(2014, 8, 1), 1) //=> Sat Feb 01 2014 00:00:00
function setQuarter
setQuarter: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, quarter: number, options?: SetQuarterOptions<ResultDate>) => ResultDate;
setQuarter Quarter Helpers Set the year quarter to the given date.
Set the year quarter to the given date.
Parameter date
The date to be changed
Parameter quarter
The quarter of the new date
Parameter options
The options
Returns
The new date with the quarter set
Example 1
// Set the 2nd quarter to 2 July 2014: const result = setQuarter(new Date(2014, 6, 2), 2) //=> Wed Apr 02 2014 00:00:00
function setSeconds
setSeconds: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, seconds: number, options?: SetSecondsOptions<ResultDate>) => ResultDate;
setSeconds Second Helpers Set the seconds to the given date, with context support.
Set the seconds to the given date, with an optional context for time zone specification.
Parameter date
The date to be changed
Parameter seconds
The seconds of the new date
Parameter options
An object with options
Returns
The new date with the seconds set
Example 1
// Set 45 seconds to 1 September 2014 11:30:40: const result = setSeconds(new Date(2014, 8, 1, 11, 30, 40), 45) //=> Mon Sep 01 2014 11:30:45
function setWeek
setWeek: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, week: number, options?: SetWeekOptions<ResultDate>) => ResultDate;
setWeek Week Helpers Set the local week to the given date.
Set the local week to the given date, saving the weekday number. The exact calculation depends on the values of
options.weekStartsOn
(which is the index of the first day of the week) andoptions.firstWeekContainsDate
(which is the day of January, which is always in the first week of the week-numbering year)Week numbering: https://en.wikipedia.org/wiki/Week#The_ISO_week_date_system
Parameter date
The date to be changed
Parameter week
The week of the new date
Parameter options
An object with options
Returns
The new date with the local week set
Example 1
// Set the 1st week to 2 January 2005 with default options: const result = setWeek(new Date(2005, 0, 2), 1) //=> Sun Dec 26 2004 00:00:00
Example 2
// Set the 1st week to 2 January 2005, // if Monday is the first day of the week, // and the first week of the year always contains 4 January: const result = setWeek(new Date(2005, 0, 2), 1, { weekStartsOn: 1, firstWeekContainsDate: 4 }) //=> Sun Jan 4 2004 00:00:00
function setWeekYear
setWeekYear: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, weekYear: number, options?: SetWeekYearOptions<ResultDate>) => ResultDate;
setWeekYear Week-Numbering Year Helpers Set the local week-numbering year to the given date.
Set the local week-numbering year to the given date, saving the week number and the weekday number. The exact calculation depends on the values of
options.weekStartsOn
(which is the index of the first day of the week) andoptions.firstWeekContainsDate
(which is the day of January, which is always in the first week of the week-numbering year)Week numbering: https://en.wikipedia.org/wiki/Week#The_ISO_week_date_system
Parameter date
The date to be changed
Parameter weekYear
The local week-numbering year of the new date
Parameter options
An object with options
Returns
The new date with the local week-numbering year set
Example 1
// Set the local week-numbering year 2004 to 2 January 2010 with default options: const result = setWeekYear(new Date(2010, 0, 2), 2004) //=> Sat Jan 03 2004 00:00:00
Example 2
// Set the local week-numbering year 2004 to 2 January 2010, // if Monday is the first day of week // and 4 January is always in the first week of the year: const result = setWeekYear(new Date(2010, 0, 2), 2004, { weekStartsOn: 1, firstWeekContainsDate: 4 }) //=> Sat Jan 01 2005 00:00:00
function setYear
setYear: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, year: number, options?: SetYearOptions<ResultDate> | undefined) => ResultDate;
setYear Year Helpers Set the year to the given date.
Set the year to the given date.
Parameter date
The date to be changed
Parameter year
The year of the new date
Parameter options
An object with options.
Returns
The new date with the year set
Example 1
// Set year 2013 to 1 September 2014: const result = setYear(new Date(2014, 8, 1), 2013) //=> Sun Sep 01 2013 00:00:00
function startOfDay
startOfDay: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, options?: StartOfDayOptions<ResultDate> | undefined) => ResultDate;
startOfDay Day Helpers Return the start of a day for the given date.
Return the start of a day for the given date. The result will be in the local timezone.
Parameter date
The original date
Parameter options
The options
Returns
The start of a day
Example 1
// 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 startOfDecade
startOfDecade: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, options?: StartOfDecadeOptions<ResultDate> | undefined) => ResultDate;
startOfDecade Decade Helpers Return the start of a decade for the given date.
Return the start of a decade for the given date.
Parameter date
The original date
Parameter options
An object with options
Returns
The start of a decade
Example 1
// The start of a decade for 21 October 2015 00:00:00: const result = startOfDecade(new Date(2015, 9, 21, 00, 00, 00)) //=> Jan 01 2010 00:00:00
function startOfHour
startOfHour: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, options?: StartOfHourOptions<ResultDate> | undefined) => ResultDate;
startOfHour Hour Helpers Return the start of an hour for the given date.
Return the start of an hour for the given date. The result will be in the local timezone.
Parameter date
The original date
Parameter options
An object with options
Returns
The start of an hour
Example 1
// The start of an hour for 2 September 2014 11:55:00: const result = startOfHour(new Date(2014, 8, 2, 11, 55)) //=> Tue Sep 02 2014 11:00:00
function startOfISOWeek
startOfISOWeek: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, options?: StartOfISOWeekOptions<ResultDate> | undefined) => ResultDate;
startOfISOWeek ISO Week Helpers Return the start of an ISO week for the given date.
Return the start of an ISO week for the given date. The result will be in the local timezone.
ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_date
Parameter date
The original date
Parameter options
An object with options
Returns
The start of an ISO week
Example 1
// The start of an ISO week for 2 September 2014 11:55:00: const result = startOfISOWeek(new Date(2014, 8, 2, 11, 55, 0)) //=> Mon Sep 01 2014 00:00:00
function startOfISOWeekYear
startOfISOWeekYear: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, options?: StartOfISOWeekYearOptions<ResultDate> | undefined) => ResultDate;
startOfISOWeekYear ISO Week-Numbering Year Helpers Return the start of an ISO week-numbering year for the given date.
Return the start of an ISO week-numbering year, which always starts 3 days before the year's first Thursday. The result will be in the local timezone.
ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_date
Parameter date
The original date
Parameter options
An object with options
Returns
The start of an ISO week-numbering year
Example 1
// The start of an ISO week-numbering year for 2 July 2005: const result = startOfISOWeekYear(new Date(2005, 6, 2)) //=> Mon Jan 03 2005 00:00:00
function startOfMinute
startOfMinute: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, options?: StartOfMinuteOptions<ResultDate> | undefined) => ResultDate;
startOfMinute Minute Helpers Return the start of a minute for the given date.
Return the start of a minute for the given date. The result will be in the local timezone.
Parameter date
The original date
Parameter options
An object with options
Returns
The start of a minute
Example 1
// The start of a minute for 1 December 2014 22:15:45.400: const result = startOfMinute(new Date(2014, 11, 1, 22, 15, 45, 400)) //=> Mon Dec 01 2014 22:15:00
function startOfMonth
startOfMonth: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, options?: StartOfMonthOptions<ResultDate> | undefined) => ResultDate;
startOfMonth Month Helpers Return the start of a month for the given date.
Return the start of a month for the given date. The result will be in the local timezone.
Parameter date
The original date
Parameter options
An object with options
Returns
The start of a month
Example 1
// 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 startOfQuarter
startOfQuarter: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, options?: StartOfQuarterOptions<ResultDate> | undefined) => ResultDate;
startOfQuarter Quarter Helpers Return the start of a year quarter for the given date.
Return the start of a year quarter for the given date. The result will be in the local timezone.
Parameter date
The original date
Parameter options
The options
Returns
The start of a quarter
Example 1
// 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 startOfSecond
startOfSecond: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, options?: StartOfSecondOptions<ResultDate> | undefined) => ResultDate;
startOfSecond Second Helpers Return the start of a second for the given date.
Return the start of a second for the given date. The result will be in the local timezone.
Parameter date
The original date
Parameter options
The options
Returns
The start of a second
Example 1
// The start of a second for 1 December 2014 22:15:45.400: const result = startOfSecond(new Date(2014, 11, 1, 22, 15, 45, 400)) //=> Mon Dec 01 2014 22:15:45.000
function startOfToday
startOfToday: <ContextDate extends Date>( options?: StartOfTodayOptions<ContextDate> | undefined) => ContextDate;
startOfToday Day Helpers Return the start of today. false
Return the start of today.
Parameter options
An object with options
Returns
The start of today
Example 1
// If today is 6 October 2014: const result = startOfToday() //=> Mon Oct 6 2014 00:00:00
function startOfTomorrow
startOfTomorrow: <ContextDate extends Date>( options?: StartOfTomorrowOptions<ContextDate> | undefined) => ContextDate;
startOfTomorrow Day Helpers Return the start of tomorrow. false
Parameter options
An object with options
Returns
The start of tomorrow
Return the start of tomorrow.
Example 1
// If today is 6 October 2014: const result = startOfTomorrow() //=> Tue Oct 7 2014 00:00:00
function startOfWeek
startOfWeek: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, options?: StartOfWeekOptions<ResultDate>) => ResultDate;
startOfWeek Week Helpers Return the start of a week for the given date.
Return the start of a week for the given date. The result will be in the local timezone.
Parameter date
The original date
Parameter options
An object with options
Returns
The start of a week
Example 1
// The start of a week for 2 September 2014 11:55:00: const result = startOfWeek(new Date(2014, 8, 2, 11, 55, 0)) //=> Sun Aug 31 2014 00:00:00
Example 2
// If the week starts on Monday, the start of the week for 2 September 2014 11:55:00: const result = startOfWeek(new Date(2014, 8, 2, 11, 55, 0), { weekStartsOn: 1 }) //=> Mon Sep 01 2014 00:00:00
function startOfWeekYear
startOfWeekYear: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, options?: StartOfWeekYearOptions<ResultDate>) => ResultDate;
startOfWeekYear Week-Numbering Year Helpers Return the start of a local week-numbering year for the given date.
Return the start of a local week-numbering year. The exact calculation depends on the values of
options.weekStartsOn
(which is the index of the first day of the week) andoptions.firstWeekContainsDate
(which is the day of January, which is always in the first week of the week-numbering year)Week numbering: https://en.wikipedia.org/wiki/Week#The_ISO_week_date_system
Parameter date
The original date
Parameter options
An object with options
Returns
The start of a week-numbering year
Example 1
// The start of an a week-numbering year for 2 July 2005 with default settings: const result = startOfWeekYear(new Date(2005, 6, 2)) //=> Sun Dec 26 2004 00:00:00
Example 2
// The start of a week-numbering year for 2 July 2005 // if Monday is the first day of week // and 4 January is always in the first week of the year: const result = startOfWeekYear(new Date(2005, 6, 2), { weekStartsOn: 1, firstWeekContainsDate: 4 }) //=> Mon Jan 03 2005 00:00:00
function startOfYear
startOfYear: <DateType extends Date, ResultDate extends Date = DateType>( date: DateArg<DateType>, options?: StartOfYearOptions<ResultDate> | undefined) => ResultDate;
startOfYear Year Helpers Return the start of a year for the given date.
Return the start of a year for the given date. The result will be in the local timezone.
Parameter date
The original date
Parameter options
The options
Returns
The start of a year
Example 1
// 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 startOfYesterday
startOfYesterday: <ContextDate extends Date>( options?: StartOfYesterdayOptions