date-fns
- Version 3.6.0
- Published
- 22.2 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
- Day
- DurationUnit
- Era
- FirstWeekContainsDate
- FormatDistanceFn
- FormatDistanceLocale
- FormatDistanceStrictUnit
- FormatDistanceToken
- FormatDistanceTokenFn
- FormatLongFn
- FormatLongWidth
- FormatRelativeFn
- FormatRelativeToken
- FormatRelativeTokenFn
- FPArity
- FPFn
- FPFnInput
- IntlFormatDistanceUnit
- IntlFormatFormatOptions
- IntlFormatLocale
- ISOStringFormat
- ISOStringRepresentation
- LocaleDayPeriod
- LocaleUnit
- LocaleUnitValue
- LocaleWidth
- LocalizeFn
- MatchFn
- MatchValueCallback
- Month
- NearestHours
- NearestMinutes
- NearestMinutesOptions
- 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>( date: DateType | number | string, duration: Duration) => DateType;
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.
| Key | Description | |----------------|------------------------------------| | years | Amount of years to be added | | months | Amount of months to be added | | weeks | Amount of weeks to be added | | days | Amount of days to be added | | hours | Amount of hours to be added | | minutes | Amount of minutes to be added | | seconds | Amount of seconds to be added |
All values default to 0
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,\-7 minutes: 9, seconds: 30, }) //=> Thu Jun 15 2017 15:29:20
function addBusinessDays
addBusinessDays: <DateType extends Date>( date: DateType | number | string, amount: number) => DateType;
addBusinessDays Date Extension 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.
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>( date: DateType | number | string, amount: number) => DateType;
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.
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>( date: DateType | number | string, amount: number) => DateType;
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.
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>( date: DateType | number | string, amount: number) => DateType;
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.
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 Jn 26 2015 00:00:00
function addMilliseconds
addMilliseconds: <DateType extends Date>( date: DateType | number | string, amount: number) => DateType;
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.
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>( date: DateType | number | string, amount: number) => DateType;
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.
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>( date: DateType | number | string, amount: number) => DateType;
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.
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>( date: DateType | number | string, amount: number) => DateType;
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.
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>( date: DateType | number | string, amount: number) => DateType;
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.
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>( date: DateType | number | string, amount: number) => DateType;
addWeeks Week Helpers Add the specified number of weeks to the given date.
Add the specified number of week to the given date.
Parameter date
The date to be changed
Parameter amount
The amount of weeks to be added.
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>( date: DateType | number | string, amount: number) => DateType;
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.
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) } ) //=> false
Example 5
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 Date>( date: DateType | number | string, interval: Interval) => DateType | Date;
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
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: <DateType extends Date>( dateToCompare: DateType | number | string, dates: Array<DateType | number | string>) => 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: <DateType extends Date>( dateToCompare: DateType | number | string, dates: Array<DateType | number | string>) => DateType | 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: <DateType extends Date>( dateLeft: DateType | number | string, dateRight: DateType | number | string) => 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: <DateType extends Date>( dateLeft: DateType | number | string, dateRight: DateType | number | string) => 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>( date: DateType | number | string, value: Date | number | string) => DateType;
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.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 'date-fns'
// A function that clones a date preserving the original type function cloneDate<DateType extends Date(date: DateType): DateType { return constructFrom( date, // Use contrustor from the given date date.getTime() // Use the date value to create a new date ) }
function constructNow
constructNow: <DateType extends Date>( date: DateType | number | string) => DateType;
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: DateType | number | string, ): 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: <DateType extends Date>( dateLeft: DateType | number | string, dateRight: DateType | number | string) => 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 arent in the weekend. Like
differenceInCalendarDays
, the function removes the times from the dates before calculating the difference.Parameter dateLeft
The later date
Parameter dateRight
The earlier date
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: <DateType extends Date>( dateLeft: DateType | number | string, dateRight: DateType | number | string) => 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 dateLeft
The later date
Parameter dateRight
The earlier date
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: <DateType extends Date>( dateLeft: DateType | number | string, dateRight: DateType | number | string) => 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 dateLeft
The later date
Parameter dateRight
The earlier date
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: <DateType extends Date>( dateLeft: DateType | number | string, dateRight: DateType | number | string) => 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 dateLeft
The later date
Parameter dateRight
The earlier date
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: <DateType extends Date>( dateLeft: DateType | number | string, dateRight: DateType | number | string) => 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 dateLeft
The later date
Parameter dateRight
The earlier date
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: <DateType extends Date>( dateLeft: DateType | number | string, dateRight: DateType | number | string) => 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 dateLeft
The later date
Parameter dateRight
The earlier date
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: <DateType extends Date>( dateLeft: DateType | number | string, dateRight: DateType | number | string, options?: DifferenceInCalendarWeeksOptions) => 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 dateLeft
The later date
Parameter dateRight
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: <DateType extends Date>( dateLeft: DateType | number | string, dateRight: DateType | number | string) => 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 dateLeft
The later date
Parameter dateRight
The earlier date
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: <DateType extends Date>( dateLeft: DateType | number | string, dateRight: DateType | number | string) => 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 dateLeft
The later date
Parameter dateRight
The earlier date
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: <DateType extends Date>( dateLeft: DateType | number | string, dateRight: DateType | number | string, 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 dateLeft
The later date
Parameter dateRight
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: <DateType extends Date>( dateLeft: DateType | number | string, dateRight: DateType | number | string) => 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 dateLeft
The later date
Parameter dateRight
The earlier date
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: <DateType extends Date>( dateLeft: DateType | number | string, dateRight: DateType | number | string) => number;
differenceInMilliseconds Millisecond Helpers Get the number of milliseconds between the given dates.
Get the number of milliseconds between the given dates.
Parameter dateLeft
The later date
Parameter dateRight
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: <DateType extends Date>( dateLeft: DateType | number | string, dateRight: DateType | number | string, 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: <DateType extends Date>( dateLeft: DateType | number | string, dateRight: DateType | number | string) => number;
differenceInMonths Month Helpers Get the number of full months between the given dates.
Get the number of full months between the given dates using trunc as a default rounding method.
Parameter dateLeft
The later date
Parameter dateRight
The earlier date
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: <DateType extends Date>( dateLeft: DateType | number | string, dateRight: DateType | number | string, options?: DifferenceInQuartersOptions) => number;
differenceInQuarters Quarter Helpers Get the number of quarters between the given dates.
Get the number of quarters between the given dates.
Parameter dateLeft
The later date
Parameter dateRight
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: <DateType extends Date>( dateLeft: DateType | number | string, dateRight: DateType | number | string, 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 dateLeft
The later date
Parameter dateRight
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: <DateType extends Date>( dateLeft: DateType | number | string, dateRight: DateType | number | string, options?: DifferenceInWeeksOptions) => 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 dateLeft
The later date
Parameter dateRight
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: <DateType extends Date>( dateLeft: DateType | number | string, dateRight: DateType | number | string) => 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 dateLeft
The later date
Parameter dateRight
The earlier date
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: <DateType extends Date>( interval: Interval<DateType>, options?: EachDayOfIntervalOptions) => DateType[];
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: <DateType extends Date>( interval: Interval<DateType>, options?: EachHourOfIntervalOptions) => DateType[];
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: <DateType extends Date>( interval: Interval<DateType>, options?: EachMinuteOfIntervalOptions) => DateType[];
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: <DateType extends Date>( interval: Interval<DateType>, options?: EachMonthOfIntervalOptions) => DateType[];
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
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: <DateType extends Date>( interval: Interval<DateType>, options?: EachQuarterOfIntervalOptions) => DateType[];
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
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: <DateType extends Date>( interval: Interval<DateType>) => DateType[];
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
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>(date: DateType) => DateType[];
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
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>( date: DateType | number | string) => DateType[];
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
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: <DateType extends Date>( interval: Interval<DateType>, options?: EachWeekOfIntervalOptions) => DateType[];
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: <DateType extends Date>( interval: Interval<DateType>, options?: EachYearOfIntervalOptions) => DateType[];
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.
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>(date: DateType | number | string) => DateType;
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
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>( date: DateType | number | string) => DateType;
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
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>(date: DateType | number | string) => DateType;
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
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>( date: DateType | number | string) => DateType;
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
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>( date: DateType | number | string) => DateType;
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
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>( date: DateType | number | string) => DateType;
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.
Parameter date
The original date
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>( date: DateType | number | string) => DateType;
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
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>( date: DateType | number | string) => DateType;
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
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>( date: DateType | number | string) => DateType;
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.
Parameter date
The original date
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: () => Date;
endOfToday Day Helpers Return the end of today. false
Return the end of today.
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: () => Date;
endOfTomorrow Day Helpers Return the end of tomorrow. false
Return the end of tomorrow.
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>( date: DateType | number | string, options?: EndOfWeekOptions) => DateType;
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>(date: DateType | number | string) => DateType;
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
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, 00)) //=> Wed Dec 31 2014 23:59:59.999
function endOfYesterday
endOfYesterday: () => Date;
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: <DateType extends Date>( date: DateType | number | string, 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: <DateType extends Date>( date: DateType | number | string, 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: <DateType extends Date>( date: DateType | number | string, baseDate: DateType | number | string, 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 date
The date
Parameter baseDate
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: <DateType extends Date>( date: DateType | number | string, baseDate: DateType | number | string, 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 date
The date
Parameter baseDate
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: <DateType extends Date>( date: DateType | number | string, 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: <DateType extends Date>( date: DateType | number | string, 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: <DateType extends Date>( date: DateType | number | string, 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 loca.l 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: <DateType extends Date>( date: DateType | number | string, 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: <DateType extends Date>( date: DateType | number | string, baseDate: DateType | number | string, 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: <DateType extends Date>( date: DateType | number | string, 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: <DateType extends Date>( date: DateType | number | string) => 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: (unixTime: number) => Date;
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)
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: <DateType extends Date>(date: DateType | number | string) => 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
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: <DateType extends Date>(date: DateType | number | string) => 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
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: <DateType extends Date>( date: DateType | number | string) => 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
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: <DateType extends Date>( date: DateType | number | string) => 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.
Parameter date
The given date
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: <DateType extends Date>( date: DateType | number | string) => 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
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: <DateType extends Date>(date: DateType | number | string) => number;
getDecade Decade Helpers Get the decade of the given date.
Get the decade of the given date.
Parameter date
The given date
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: <DateType extends Date>(date: DateType | number | string) => number;
getHours Hour Helpers Get the hours of the given date.
Get the hours of the given date.
Parameter date
The given date
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: <DateType extends Date>(date: DateType | number | string) => 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
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: <DateType extends Date>(date: DateType | number | string) => 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
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: <DateType extends Date>( date: DateType | number | string) => 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
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: <DateType extends Date>( date: DateType | number | string) => 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: <DateType extends Date>( date: DateType | number | string) => 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: <DateType extends Date>(date: DateType | number | string) => number;
getMinutes Minute Helpers Get the minutes of the given date.
Get the minutes of the given date.
Parameter date
The given date
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: <DateType extends Date>(date: DateType | number | string) => number;
getMonth Month Helpers Get the month of the given date.
Get the month of the given date.
Parameter date
The given date
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: <DateType extends Date>( intervalLeft: Interval<DateType>, intervalRight: Interval<DateType>) => 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.
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: <DateType extends Date>(date: DateType | number | string) => 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
Returns
The quarter
Example 1
// Which quarter is 2 July 2014? const result = getQuarter(new Date(2014, 6, 2)) //=> 3
function getSeconds
getSeconds: <DateType extends Date>(date: DateType | number | string) => 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: <DateType extends Date>(date: DateType | number | string) => 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: <DateType extends Date>(date: DateType | number | string) => 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: <DateType extends Date>( date: DateType | number | string, options?: GetWeekOptions) => 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: <DateType extends Date>( date: DateType | number | string, 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: <DateType extends Date>( date: DateType | number | string, options?: GetWeeksInMonthOptions) => 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: <DateType extends Date>( date: DateType | number | string, 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: <DateType extends Date>(date: DateType | number | string) => number;
getYear Year Helpers Get the year of the given date.
Get the year of the given date.
Parameter date
The given date
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: <DateType extends Date>( start: DateType | number | string, end: DateType | number | string, options?: IntervalOptions) => NormalizedInterval<DateType>;
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: <DateType extends Date>( interval: Interval<DateType>) => Duration;
intervalToDuration Common Helpers Convert interval to duration
Convert a interval object to a duration object.
Parameter interval
The interval to convert to duration
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: { <DateType extends Date>(date: DateType | number | string): string; <DateType extends Date>( date: string | number | DateType, localeOptions: IntlFormatLocaleOptions ): string; <DateType extends Date>( date: string | number | DateType, formatOptions: Intl.DateTimeFormatOptions ): string; <DateType extends Date>( date: string | number | DateType, 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: <DateType extends Date>( date: DateType | number | string, baseDate: DateType | number | string, 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 date
The date
Parameter baseDate
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: <DateType extends Date>( date: DateType | number | string, dateToCompare: DateType | number | string) => 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: <DateType extends Date>( date: DateType | number | string, dateToCompare: DateType | number | string) => 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: <DateType extends Date>( leftDate: DateType | number | string, rightDate: DateType | number | string) => 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: <DateType extends Date>( date: DateType | number | string) => 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
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: <DateType extends Date>(date: DateType | number | string) => boolean;
isFriday Weekday Helpers Is the given date Friday?
Is the given date Friday?
Parameter date
The date to check
Returns
The date is Friday
Example 1
// Is 26 September 2014 Friday? const result = isFriday(new Date(2014, 8, 26)) //=> true
function isFuture
isFuture: <DateType extends Date>(date: DateType | number | string) => 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: <DateType extends Date>( date: DateType | number | string) => 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
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: <DateType extends Date>(date: DateType | number | string) => 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
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 interchangably.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 ofthen 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: <DateType extends Date>(date: DateType | number | string) => boolean;
isMonday Weekday Helpers Is the given date Monday?
Is the given date Monday?
Parameter date
The date to check
Returns
The date is Monday
Example 1
// Is 22 September 2014 Monday? const result = isMonday(new Date(2014, 8, 22)) //=> true
function isPast
isPast: <DateType extends Date>(date: DateType | number | string) => 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: <DateType extends Date>( dateLeft: DateType | number | string, dateRight: DateType | number | string) => 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 dateLeft
The first date to check
Parameter dateRight
The second date to check
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: <DateType extends Date>( dateLeft: DateType | number | string, dateRight: DateType | number | string) => 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
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: <DateType extends Date>( dateLeft: DateType | number | string, dateRight: DateType | number | string) => 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 dateLeft
The first date to check
Parameter dateRight
The second date to check
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: <DateType extends Date>( dateLeft: DateType | number | string, dateRight: DateType | number | string) => 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 dateLeft
The first date to check
Parameter dateRight
The second date to check
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: <DateType extends Date>( dateLeft: DateType | number | string, dateRight: DateType | number | string) => 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 dateLeft
The first date to check
Parameter dateRight
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: <DateType extends Date>( dateLeft: DateType | number | string, dateRight: DateType | number | string) => 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 dateLeft
The first date to check
Parameter dateRight
The second date to check
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: <DateType extends Date>( dateLeft: DateType | number | string, dateRight: DateType | number | string) => 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 dateLeft
The first date to check
Parameter dateRight
The second date to check
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: <DateType extends Date>( dateLeft: DateType | number | string, dateRight: DateType | number | string) => 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 dateLeft
The first date to check
Parameter dateRight
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: <DateType extends Date>( dateLeft: DateType | number | string, dateRight: DateType | number | string, 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 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 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: <DateType extends Date>( dateLeft: DateType | number | string, dateRight: DateType | number | string) => boolean;
isSameYear Year Helpers Are the given dates in the same year?
Are the given dates in the same year?
Parameter dateLeft
The first date to check
Parameter dateRight
The second date to check
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: <DateType extends Date>(date: DateType | number | string) => boolean;
isSaturday Weekday Helpers Is the given date Saturday?
Is the given date Saturday?
Parameter date
The date to check
Returns
The date is Saturday
Example 1
// Is 27 September 2014 Saturday? const result = isSaturday(new Date(2014, 8, 27)) //=> true
function isSunday
isSunday: <DateType extends Date>(date: DateType | number | string) => boolean;
isSunday Weekday Helpers Is the given date Sunday?
Is the given date Sunday?
Parameter date
The date to check
Returns
The date is Sunday
Example 1
// Is 21 September 2014 Sunday? const result = isSunday(new Date(2014, 8, 21)) //=> true
function isThisHour
isThisHour: <DateType extends Date>(date: DateType | number | string) => 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
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: <DateType extends Date>( date: DateType | number | string) => 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
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: <DateType extends Date>( date: DateType | number | string) => 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: <DateType extends Date>( date: DateType | number | string) => 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
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: <DateType extends Date>( date: DateType | number | string) => 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
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: <DateType extends Date>( date: DateType | number | string) => 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: <DateType extends Date>( date: DateType | number | string, 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: <DateType extends Date>(date: DateType | number | string) => 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
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: <DateType extends Date>(date: DateType | number | string) => boolean;
isThursday Weekday Helpers Is the given date Thursday?
Is the given date Thursday?
Parameter date
The date to check
Returns
The date is Thursday
Example 1
// Is 25 September 2014 Thursday? const result = isThursday(new Date(2014, 8, 25)) //=> true
function isToday
isToday: <DateType extends Date>(date: DateType | number | string) => boolean;
isToday Day Helpers Is the given date today? false
Is the given date today?
Parameter date
The date to check
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: <DateType extends Date>(date: DateType | number | string) => boolean;
isTomorrow Day Helpers Is the given date tomorrow? false
Is the given date tomorrow?
Parameter date
The date to check
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: <DateType extends Date>(date: DateType | number | string) => boolean;
isTuesday Weekday Helpers Is the given date Tuesday?
Is the given date Tuesday?
Parameter date
The date to check
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, convertable into a date: const result = isValid(1393804800000) //=> true
Example 3
// For the invalid date: const result = isValid(new Date('')) //=> false
function isWednesday
isWednesday: <DateType extends Date>( date: DateType | number | string) => boolean;
isWednesday Weekday Helpers Is the given date Wednesday?
Is the given date Wednesday?
Parameter date
The date to check
Returns
The date is Wednesday
Example 1
// Is 24 September 2014 Wednesday? const result = isWednesday(new Date(2014, 8, 24)) //=> true
function isWeekend
isWeekend: <DateType extends Date>(date: DateType | number | string) => boolean;
isWeekend Weekday Helpers Does the given date fall on a weekend?
Does the given date fall on a weekend?
Parameter date
The date to check
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: <DateType extends Date>( date: DateType | number | string, interval: Interval<DateType>) => 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
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 interval start: isWithinInterval(date, { start, end: date }) // => true
Example 4
// For date equal to interval end: isWithinInterval(date, { start: date, end }) // => true
function isYesterday
isYesterday: <DateType extends Date>( date: DateType | number | string) => boolean;
isYesterday Day Helpers Is the given date yesterday? false
Is the given date yesterday?
Parameter date
The date to check
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>( date: DateType | number | string) => DateType;
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
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>( date: DateType | number | string) => DateType;
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
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>( date: DateType | number | string) => DateType;
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
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>( date: DateType | number | string) => DateType;
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
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>( date: DateType | number | string) => DateType;
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
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>( date: DateType | number | string, options?: LastDayOfWeekOptions) => DateType;
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.
Parameter date
The original date
Parameter options
An object with options
Returns
The last day of a week
Example 1
// The last day of a week for 2 September 2014 11:55:00: const result = lastDayOfWeek(new Date(2014, 8, 2, 11, 55, 0)) //=> Sat Sep 06 2014 00:00:00
Example 2
// If the week starts on Monday, the last day of the week for 2 September 2014 11:55:00: const result = lastDayOfWeek(new Date(2014, 8, 2, 11, 55, 0), { weekStartsOn: 1 }) //=> Sun Sep 07 2014 00:00:00
function lastDayOfYear
lastDayOfYear: <DateType extends Date>( date: DateType | number | string) => DateType;
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
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: <DateType extends Date>( date: DateType | number | string, 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>( dates: Array<DateType | number | string>) => DateType | Date;
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 occures every 4 years, except for years that are divisable by 100 and not divisable 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 miliseconds to seconds: const result = millisecondsToSeconds(1000) //=> 1
Example 2
// It uses floor rounding: const result = millisecondsToSeconds(1999) //=> 1
function min
min: <DateType extends Date>( dates: Array<DateType | number | string>) => DateType | Date;
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>( date: DateType | number | string, day: Day) => DateType;
nextDay Weekday Helpers When is the next day of the week?
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
Returns
The date is the next day of 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>( date: DateType | number | string) => DateType;
nextFriday Weekday Helpers When is the next Friday?
When is the next Friday?
Parameter date
The date to start counting from
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>( date: DateType | number | string) => DateType;
nextMonday Weekday Helpers When is the next Monday?
When is the next Monday?
Parameter date
The date to start counting from
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>( date: DateType | number | string) => DateType;
nextSaturday Weekday Helpers When is the next Saturday?
When is the next Saturday?
Parameter date
The date to start counting from
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>( date: DateType | number | string) => DateType;
nextSunday Weekday Helpers When is the next Sunday?
When is the next Sunday?
Parameter date
The date to start counting from
Returns
The next Sunday
Example 1
// When is the next Sunday after Mar, 22, 2020? const result = nextSunday(new Date(2020, 2, 22)) //=> Sun Mar 29 2020 00:00:00
function nextThursday
nextThursday: <DateType extends Date>( date: DateType | number | string) => DateType;
nextThursday Weekday Helpers When is the next Thursday?
When is the next Thursday?
Parameter date
The date to start counting from
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>( date: DateType | number | string) => DateType;
nextTuesday Weekday Helpers When is the next Tuesday?
When is the next Tuesday?
Parameter date
The date to start counting from
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>( date: DateType | number | string) => DateType;
nextWednesday Weekday Helpers When is the next Wednesday?
When is the next Wednesday?
Parameter date
The date to start counting from
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>( dateStr: string, formatStr: string, referenceDate: DateType | number | string, options?: ParseOptions) => DateType;
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 interchangably.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 ofthen 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: (argument: string, options?: ParseISOOptions) => Date;
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: (dateStr: string) => Date;
parseJSON Common Helpers Parse a JSON date string
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
Returns
The parsed date in the local time zone
function previousDay
previousDay: <DateType extends Date>( date: DateType | number | string, day: Day) => DateType;
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
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>( date: DateType | number | string) => DateType;
previousFriday Weekday Helpers When is the previous Friday?
When is the previous Friday?
Parameter date
The date to start counting from
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>( date: DateType | number | string) => DateType;
previousMonday Weekday Helpers When is the previous Monday?
When is the previous Monday?
Parameter date
The date to start counting from
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>( date: DateType | number | string) => DateType;
previousSaturday Weekday Helpers When is the previous Saturday?
When is the previous Saturday?
Parameter date
The date to start counting from
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>( date: DateType | number | string) => DateType;
previousSunday Weekday Helpers When is the previous Sunday?
When is the previous Sunday?
Parameter date
The date to start counting from
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>( date: DateType | number | string) => DateType;
previousThursday Weekday Helpers When is the previous Thursday?
When is the previous Thursday?
Parameter date
The date to start counting from
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>( date: DateType | number | string) => DateType;
previousTuesday Weekday Helpers When is the previous Tuesday?
When is the previous Tuesday?
Parameter date
The date to start counting from
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>( date: DateType | number | string) => DateType;
previousWednesday Weekday Helpers When is the previous Wednesday?
When is the previous Wednesday?
Parameter date
The date to start counting from
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>( date: DateType | number | string, options?: RoundToNearestHoursOptions) => Date;
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>( date: DateType | number | string, options?: RoundToNearestMinutesOptions) => DateType;
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>( date: DateType | number | string, values: DateValues) => DateType;
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
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>( date: DateType | number | string, dayOfMonth: number) => DateType;
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
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>( date: DateType | number | string, day: number, options?: SetDayOptions) => DateType;
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>( date: DateType | number | string, dayOfYear: number) => DateType;
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
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>( date: DateType | number | string, hours: number) => DateType;
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
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>( date: DateType | number | string, day: number) => DateType;
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
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>( date: DateType | number | string, week: number) => DateType;
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
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>( date: DateType | number | string, weekYear: number) => DateType;
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
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>( date: DateType | number | string, milliseconds: number) => DateType;
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
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>( date: DateType | number | string, minutes: number) => DateType;
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
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>( date: DateType | number | string, month: number) => DateType;
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)
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>( date: DateType | number | string, quarter: number) => DateType;
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
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>( date: DateType | number | string, seconds: number) => DateType;
setSeconds Second Helpers Set the seconds to the given date.
Set the seconds to the given date.
Parameter date
The date to be changed
Parameter seconds
The seconds of the new date
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>( date: DateType | number | string, week: number, options?: SetWeekOptions) => DateType;
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>( date: DateType | number | string, weekYear: number, options?: SetWeekYearOptions) => DateType;
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>( date: DateType | number | string, year: number) => DateType;
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
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>( date: DateType | number | string) => DateType;
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
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>( date: DateType | number | string) => DateType;
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
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>( date: DateType | number | string) => DateType;
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
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>( date: DateType | number | string) => DateType;
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
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>( date: DateType | number | string) => DateType;
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
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>( date: DateType | number | string) => DateType;
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
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>( date: DateType | number | string) => DateType;
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
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>( date: DateType | number | string) => DateType;
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
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>( date: DateType | number | string) => DateType;
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
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: () => Date;
startOfToday Day Helpers Return the start of today. false
Return the start of today.
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: () => Date;
startOfTomorrow Day Helpers Return the start of tomorrow. false
Return the start of tomorrow.
Returns
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>( date: DateType | number | string, options?: StartOfWeekOptions) => DateType;
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>( date: DateType | number | string, options?: StartOfWeekYearOptions) => DateType;
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>( date: DateType | number | string) => DateType;
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
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: () => Date;
startOfYesterday Day Helpers Return the start of yesterday. false
Return the start of yesterday.
Returns
The start of yesterday
Example 1
// If today is 6 October 2014: const result = startOfYesterday() //=> Sun Oct 5 2014 00:00:00
function sub
sub: <DateType extends Date>( date: DateType | number | string, duration: Duration) => DateType;
sub Common Helpers Subtract the specified years, months, weeks, days, hours, minutes and seconds from the given date.
Subtract the specified years, months, weeks, days, hours, minutes and seconds from 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 subtracted
| Key | Description | |---------|------------------------------------| | years | Amount of years to be subtracted | | months | Amount of months to be subtracted | | weeks | Amount of weeks to be subtracted | | days | Amount of days to be subtracted | | hours | Amount of hours to be subtracted | | minutes | Amount of minutes to be subtracted | | seconds | Amount of seconds to be subtracted |
All values default to 0
Returns
The new date with the seconds subtracted
Example 1
// Subtract the following duration from 15 June 2017 15:29:20 const result = sub(new Date(2017, 5, 15, 15, 29, 20), { years: 2, months: 9, weeks: 1, days: 7, hours: 5, minutes: 9, seconds: 30 }) //=> Mon Sep 1 2014 10:19:50
function subBusinessDays
subBusinessDays: <DateType extends Date>( date: DateType | number | string, amount: number) => DateType;
subBusinessDays Day Helpers Substract the specified number of business days (mon - fri) to the given date.
Substract 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 subtracted.
Returns
The new date with the business days subtracted
Example 1
// Substract 10 business days from 1 September 2014: const result = subBusinessDays(new Date(2014, 8, 1), 10) //=> Mon Aug 18 2014 00:00:00 (skipped weekend days)
function subDays
subDays: <DateType extends Date>( date: DateType | number | string, amount: number) => DateType;
subDays Day Helpers Subtract the specified number of days from the given date.
Subtract the specified number of days from the given date.
Parameter date
The date to be changed
Parameter amount
The amount of days to be subtracted.
Returns
The new date with the days subtracted
Example 1
// Subtract 10 days from 1 September 2014: const result = subDays(new Date(2014, 8, 1), 10) //=> Fri Aug 22 2014 00:00:00
function subHours
subHours: <DateType extends Date>( date: DateType | number | string, amount: number) => DateType;
subHours Hour Helpers Subtract the specified number of hours from the given date.
Subtract the specified number of hours from the given date.
Parameter date
The date to be changed
Parameter amount
The amount of hours to be subtracted.
Returns
The new date with the hours subtracted
Example 1
// Subtract 2 hours from 11 July 2014 01:00:00: const result = subHours(new Date(2014, 6, 11, 1, 0), 2) //=> Thu Jul 10 2014 23:00:00
function subISOWeekYears
subISOWeekYears: <DateType extends Date>( date: DateType | number | string, amount: number) => DateType;
subISOWeekYears ISO Week-Numbering Year Helpers Subtract the specified number of ISO week-numbering years from the given date.
Subtract the specified number of ISO week-numbering years from 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 subtracted.
Returns
The new date with the ISO week-numbering years subtracted
Example 1
// Subtract 5 ISO week-numbering years from 1 September 2014: const result = subISOWeekYears(new Date(2014, 8, 1), 5) //=> Mon Aug 31 2009 00:00:00
function subMilliseconds
subMilliseconds: <DateType extends Date>( date: DateType | number | string, amount: number) => DateType;
subMilliseconds Millisecond Helpers Subtract the specified number of milliseconds from the given date.
Subtract the specified number of milliseconds from the given date.
Parameter date
The date to be changed
Parameter amount
The amount of milliseconds to be subtracted.
Returns
The new date with the milliseconds subtracted
Example 1
// Subtract 750 milliseconds from 10 July 2014 12:45:30.000: const result = subMilliseconds(new Date(2014, 6, 10, 12, 45, 30, 0), 750) //=> Thu Jul 10 2014 12:45:29.250
function subMinutes
subMinutes: <DateType extends Date>( date: DateType | number | string, amount: number) => DateType;
subMinutes Minute Helpers Subtract the specified number of minutes from the given date.
Subtract the specified number of minutes from the given date.
Parameter date
The date to be changed
Parameter amount
The amount of minutes to be subtracted.
Returns
The new date with the minutes subtracted
Example 1
// Subtract 30 minutes from 10 July 2014 12:00:00: const result = subMinutes(new Date(2014, 6, 10, 12, 0), 30) //=> Thu Jul 10 2014 11:30:00
function subMonths
subMonths: <DateType extends Date>( date: DateType | number | string, amount: number) => DateType;
subMonths Month Helpers Subtract the specified number of months from the given date.
Subtract the specified number of months from the given date.
Parameter date
The date to be changed
Parameter amount
The amount of months to be subtracted.
Returns
The new date with the months subtracted
Example 1
// Subtract 5 months from 1 February 2015: const result = subMonths(new Date(2015, 1, 1), 5) //=> Mon Sep 01 2014 00:00:00
function subQuarters
subQuarters: <DateType extends Date>( date: DateType | number | string, amount: number) => DateType;
subQuarters Quarter Helpers Subtract the specified number of year quarters from the given date.
Subtract the specified number of year quarters from the given date.
Parameter date
The date to be changed
Parameter amount
The amount of quarters to be subtracted.
Returns
The new date with the quarters subtracted
Example 1
// Subtract 3 quarters from 1 September 2014: const result = subQuarters(new Date(2014, 8, 1), 3) //=> Sun Dec 01 2013 00:00:00
function subSeconds
subSeconds: <DateType extends Date>( date: DateType | number | string, amount: number) => DateType;
subSeconds Second Helpers Subtract the specified number of seconds from the given date.
Subtract the specified number of seconds from the given date.
Parameter date
The date to be changed
Parameter amount
The amount of seconds to be subtracted.
Returns
The new date with the seconds subtracted
Example 1
// Subtract 30 seconds from 10 July 2014 12:45:00: const result = subSeconds(new Date(2014, 6, 10, 12, 45, 0), 30) //=> Thu Jul 10 2014 12:44:30
function subWeeks
subWeeks: <DateType extends Date>( date: DateType | number | string, amount: number) => DateType;
subWeeks Week Helpers Subtract the specified number of weeks from the given date.
Subtract the specified number of weeks from the given date.
Parameter date
The date to be changed
Parameter amount
The amount of weeks to be subtracted.
Returns
The new date with the weeks subtracted
Example 1
// Subtract 4 weeks from 1 September 2014: const result = subWeeks(new Date(2014, 8, 1), 4) //=> Mon Aug 04 2014 00:00:00
function subYears
subYears: <DateType extends Date>( date: DateType | number | string, amount: number) => DateType;
subYears Year Helpers Subtract the specified number of years from the given date.
Subtract the specified number of years from the given date.
Parameter date
The date to be changed
Parameter amount
The amount of years to be subtracted.
Returns
The new date with the years subtracted
Example 1
// Subtract 5 years from 1 September 2014: const result = subYears(new Date(2014, 8, 1), 5) //=> Tue Sep 01 2009 00:00:00
function toDate
toDate: <DateType extends Date>( argument: DateType | number | string) => DateType;
toDate Common Helpers Convert the given argument to an instance of Date.
Convert the given argument to an instance of Date.
If the argument is an instance of Date, the function returns its clone.
If the argument is a number, it is treated as a timestamp.
If the argument is none of the above, the function returns Invalid Date.
**Note**: *all* Date arguments passed to any *date-fns* function is processed by
toDate
.Parameter argument
The value to convert
Returns
The parsed date in the local time zone
Example 1
// Clone the date: const result = toDate(new Date(2014, 1, 11, 11, 30, 30)) //=> Tue Feb 11 2014 11:30:30
Example 2
// Convert the timestamp to date: const result = toDate(1392098430000) //=> Tue Feb 11 2014 11:30:30
function transpose
transpose: <DateInputType extends Date, DateOutputType extends Date>( fromDate: DateInputType, constructor: DateOutputType | GenericDateConstructor<DateOutputType>) => DateOutputType;
transpose Generic Helpers Transpose the date to the given constructor.
The function transposes the date to the given constructor. It helps you to transpose the date in the system time zone to say
UTCDate
or any other date extension.Parameter fromDate
The date to use values from
Parameter constructor
The date constructor to use
Returns
Date transposed to the given constructor
Example 1
// Create July 10, 2022 00:00 in locale time zone const date = new Date(2022, 6, 10) //=> 'Sun Jul 10 2022 00:00:00 GMT+0800 (Singapore Standard Time)'
Example 2
// Transpose the date to July 10, 2022 00:00 in UTC transpose(date, UTCDate) //=> 'Sun Jul 10 2022 00:00:00 GMT+0000 (Coordinated Universal Time)'
function weeksToDays
weeksToDays: (weeks: number) => number;
weeksToDays Conversion Helpers Convert weeks to days.
Convert a number of weeks to a full number of days.
Parameter weeks
The number of weeks to be converted
Returns
The number of weeks converted in days
Example 1
// Convert 2 weeks into days const result = weeksToDays(2) //=> 14
function yearsToDays
yearsToDays: (years: number) => number;
yearsToDays Conversion Helpers Convert years to days.
Convert a number of years to a full number of days.
Parameter years
The number of years to be converted
Returns
The number of years converted in days
Example 1
// Convert 2 years into days const result = yearsToDays(2) //=> 730
function yearsToMonths
yearsToMonths: (years: number) => number;
yearsToMonths Conversion Helpers Convert years to months.
Convert a number of years to a full number of months.
Parameter years
The number of years to be converted
Returns
The number of years converted in months
Example 1
// Convert 2 years into months const result = yearsToMonths(2) //=> 24
function yearsToQuarters
yearsToQuarters: (years: number) => number;
yearsToQuarters Conversion Helpers Convert years to quarters.
Convert a number of years to a full number of quarters.
Parameter years
The number of years to be converted
Returns
The number of years converted in quarters
Example 1
// Convert 2 years to quarters const result = yearsToQuarters(2) //=> 8
Interfaces
interface AdditionalTokensOptions
interface AdditionalTokensOptions {}
Additional tokens options. Used to build function options.
property useAdditionalDayOfYearTokens
useAdditionalDayOfYearTokens?: boolean;
If true, allows usage of the day of year tokens
D
andDD
. See: https://date-fns.org/docs/Unicode-Tokens
property useAdditionalWeekYearTokens
useAdditionalWeekYearTokens?: boolean;
If true, allows usage of the week-numbering year tokens
YY
andYYYY
. See: https://date-fns.org/docs/Unicode-Tokens
interface AreIntervalsOverlappingOptions
interface AreIntervalsOverlappingOptions {}
The areIntervalsOverlapping function options.
property inclusive
inclusive?: boolean;
Whether the comparison is inclusive or not
interface DateValues
interface DateValues {}
The date values, used to set or get date object values.
property date
date?: number;
The day of the month
property hours
hours?: number;
The hours
property milliseconds
milliseconds?: number;
The milliseconds
property minutes
minutes?: number;
The minutes
property month
month?: number;
The month
property seconds
seconds?: number;
The seconds
property year
year?: number;
The year
interface DifferenceInCalendarWeeksOptions
interface DifferenceInCalendarWeeksOptions extends LocalizedOptions<'options'>, WeekOptions {}
The differenceInCalendarWeeks function options.
interface DifferenceInHoursOptions
interface DifferenceInHoursOptions extends RoundingOptions {}
The differenceInHours function options.
interface DifferenceInMinutesOptions
interface DifferenceInMinutesOptions extends RoundingOptions {}
The differenceInMinutes function options.
interface DifferenceInQuartersOptions
interface DifferenceInQuartersOptions extends RoundingOptions {}
The differenceInQuarters function options.
interface DifferenceInSecondsOptions
interface DifferenceInSecondsOptions extends RoundingOptions {}
The differenceInSeconds function options.
interface DifferenceInWeeksOptions
interface DifferenceInWeeksOptions extends RoundingOptions {}
The differenceInWeeks function options.
interface Duration
interface Duration {}
The duration object. Contains the duration in the units specified by the object.
property days
days?: number;
The number of days in the duration
property hours
hours?: number;
The number of hours in the duration
property minutes
minutes?: number;
The number of minutes in the duration
property months
months?: number;
The number of months in the duration
property seconds
seconds?: number;
The number of seconds in the duration
property weeks
weeks?: number;
The number of weeks in the duration
property years
years?: number;
The number of years in the duration
interface EachDayOfIntervalOptions
interface EachDayOfIntervalOptions extends StepOptions {}
The eachDayOfInterval function options.
interface EachHourOfIntervalOptions
interface EachHourOfIntervalOptions extends StepOptions {}
The eachHourOfInterval function options.
interface EachMinuteOfIntervalOptions
interface EachMinuteOfIntervalOptions extends StepOptions {}
The eachMinuteOfInterval function options.
interface EachMonthOfIntervalOptions
interface EachMonthOfIntervalOptions extends StepOptions {}
The eachMonthOfInterval function options.
interface EachQuarterOfIntervalOptions
interface EachQuarterOfIntervalOptions extends StepOptions {}
The eachQuarterOfInterval function options.
interface EachWeekOfIntervalOptions
interface EachWeekOfIntervalOptions extends StepOptions, WeekOptions, LocalizedOptions<'options'> {}
The eachWeekOfInterval function options.
interface EachYearOfIntervalOptions
interface EachYearOfIntervalOptions extends StepOptions {}
The eachYearOfInterval function options.
interface EndOfWeekOptions
interface EndOfWeekOptions extends WeekOptions, LocalizedOptions<'options'> {}
The endOfWeek function options.
interface FirstWeekContainsDateOptions
interface FirstWeekContainsDateOptions {}
The first week contains date options. Used to build function options.
property firstWeekContainsDate
firstWeekContainsDate?: FirstWeekContainsDate;
See FirstWeekContainsDate for more details.
interface FormatDateOptions
interface FormatOptions extends LocalizedOptions<'options' | 'localize' | 'formatLong'>, WeekOptions, FirstWeekContainsDateOptions, AdditionalTokensOptions {}
The format function options.
interface FormatDistanceFnOptions
interface FormatDistanceFnOptions {}
The FormatDistanceFn function options.
property addSuffix
addSuffix?: boolean;
Add "X ago"/"in X" in the locale language
property comparison
comparison?: -1 | 0 | 1;
The distance vector. -1 represents past and 1 future. Tells which suffix to use.
interface FormatDistanceOptions
interface FormatDistanceOptions extends LocalizedOptions<'formatDistance'> {}
The formatDistance function options.
property addSuffix
addSuffix?: boolean;
Add "X ago"/"in X" in the locale language
property includeSeconds
includeSeconds?: boolean;
Distances less than a minute are more detailed
interface FormatDistanceStrictOptions
interface FormatDistanceStrictOptions extends LocalizedOptions<'formatDistance'>, RoundingOptions {}
The formatDistanceStrict function options.
interface FormatDistanceToNowOptions
interface FormatDistanceToNowOptions extends FormatDistanceOptions {}
The formatDistanceToNow function options.
interface FormatDistanceToNowStrictOptions
interface FormatDistanceToNowStrictOptions extends FormatDistanceStrictOptions {}
The formatDistanceToNowStrict function options.
interface FormatDurationOptions
interface FormatDurationOptions extends LocalizedOptions<'formatDistance'> {}
The formatDuration function options.
interface FormatISO9075Options
interface FormatISO9075Options extends ISOFormatOptions {}
The formatISO9075 function options.
interface FormatISOOptions
interface FormatISOOptions extends ISOFormatOptions {}
The formatISO function options.
interface FormatLong
interface FormatLong {}
The object with functions that return localized formats. Long stands for sequence of tokens (i.e. PPpp) that allows to define how format both date and time at once. Part of the public locale API.
interface FormatLongFnOptions
interface FormatLongFnOptions {}
The FormatLongFn function options.
property width
width?: FormatLongWidth;
Format width to set
interface FormatOptions
interface FormatOptions extends LocalizedOptions<'options' | 'localize' | 'formatLong'>, WeekOptions, FirstWeekContainsDateOptions, AdditionalTokensOptions {}
The format function options.
interface FormatPart
interface FormatPart {}
A format part that represents a token or string literal, used by format parser/tokenizer
interface FormatRelativeFnOptions
interface FormatRelativeFnOptions extends WeekOptions, LocalizedOptions<'options' | 'formatRelative'> {}
The FormatRelativeFn function options.
interface FormatRelativeOptions
interface FormatRelativeOptions extends LocalizedOptions< 'options' | 'localize' | 'formatLong' | 'formatRelative' >, WeekOptions {}
The formatRelative function options.
interface FormatRelativeTokenFnOptions
interface FormatRelativeTokenFnOptions extends WeekOptions {}
The FormatRelativeTokenFn function options.
interface FormatRFC3339Options
interface FormatRFC3339Options {}
The formatRFC3339 function options.
property fractionDigits
fractionDigits?: 0 | 1 | 2 | 3;
The number of digits after the decimal point after seconds, defaults to 0
interface FPFn1
interface FPFn1<Result, Arg> {}
FP function interface with 1 arguments.
call signature
(): FPFn1<Result, Arg>;
Curried version of the function. Returns itself.
call signature
(arg: Arg): Result;
Returns the result of the function call.
interface FPFn2
interface FPFn2<Result, Arg2, Arg1> {}
FP function interface with 2 arguments.
call signature
(): FPFn2<Result, Arg2, Arg1>;
Curried version of the function. Returns itself.
call signature
(arg2: Arg2): FPFn1<Result, Arg1>;
Curried version of the function. Returns a function that accepts the rest arguments.
call signature
(arg2: Arg2, arg1: Arg1): Result;
Returns the result of the function call.
interface FPFn3
interface FPFn3<Result, Arg3, Arg2, Arg1> {}
FP function interface with 3 arguments.
call signature
(): FPFn3<Result, Arg3, Arg2, Arg1>;
Curried version of the function. Returns itself.
call signature
(arg3: Arg3): FPFn2<Result, Arg2, Arg1>;
Curried version of the function. Returns a function that accepts the rest arguments.
call signature
(arg3: Arg3, arg2: Arg2): FPFn1<Result, Arg1>;
Curried version of the function. Returns a function that accepts the rest arguments.
call signature
(arg3: Arg3, arg2: Arg2, arg1: Arg1): Result;
Returns the result of the function call.
interface FPFn4
interface FPFn4<Result, Arg4, Arg3, Arg2, Arg1> {}
FP function interface with 4 arguments.
call signature
(): FPFn4<Result, Arg4, Arg3, Arg2, Arg1>;
Curried version of the function. Returns itself.
call signature
(arg4: Arg4): FPFn3<Result, Arg3, Arg2, Arg1>;
Curried version of the function. Returns a function that accepts the rest arguments.
call signature
(arg4: Arg4, arg3: Arg3): FPFn2<Result, Arg2, Arg1>;
Curried version of the function. Returns a function that accepts the rest arguments.
call signature
(arg4: Arg4, arg3: Arg3, arg2: Arg2): FPFn1<Result, Arg1>;
Curried version of the function. Returns a function that accepts the rest arguments.
call signature
(arg4: Arg4, arg3: Arg3, arg2: Arg2, arg1: Arg1): Result;
Returns the result of the function call.
interface GenericDateConstructor
interface GenericDateConstructor<DateType extends Date = Date> {}
The generic date constructor. Replicates the Date constructor. Used to build generic functions.
construct signature
new (): DateType;
The date constructor. Creates date with the current date and time.
Returns
The date instance
construct signature
new (value: Date | number | string): DateType;
The date constructor. Creates date with the passed date, number of milliseconds or string to parse.
Parameter value
The date, number of milliseconds or string to parse
Returns
The date instance
construct signature
new ( year: number, month: number, date?: number, hours?: number, minutes?: number, seconds?: number, ms?: number): DateType;
The date constructor. Creates date with the passed date values (year, month, etc.) Note that the month is 0-indexed.
Parameter year
The year
Parameter month
The month. Note that the month is 0-indexed.
Parameter date
The day of the month
Parameter hours
The hours
Parameter minutes
The minutes
Parameter seconds
The seconds
Parameter ms
The milliseconds
Returns
The date instance
interface GetWeekOfMonthOptions
interface GetWeekOfMonthOptions extends LocalizedOptions<'options'>, WeekOptions {}
The getWeekOfMonth function options.
interface GetWeekOptions
interface GetWeekOptions extends LocalizedOptions<'options'>, WeekOptions, FirstWeekContainsDateOptions {}
The getWeek function options.
interface GetWeeksInMonthOptions
interface GetWeeksInMonthOptions extends LocalizedOptions<'options'>, WeekOptions {}
The getWeeksInMonth function options.
interface GetWeekYearOptions
interface GetWeekYearOptions extends LocalizedOptions<'options'>, WeekOptions, FirstWeekContainsDateOptions {}
The getWeekYear function options.
interface Interval
interface Interval<DateType extends Date = Date> {}
An object that combines two dates to represent the time interval.
interface IntervalOptions
interface IntervalOptions {}
The interval function options.
property assertPositive
assertPositive?: boolean;
Asserts that the interval is positive (start is after the end).
interface IntlFormatDistanceOptions
interface IntlFormatDistanceOptions {}
The intlFormatDistance function options.
property locale
locale?: Intl.UnicodeBCP47LocaleIdentifier | Intl.UnicodeBCP47LocaleIdentifier[];
The locale(s) to use (see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#locales_argument)
property localeMatcher
localeMatcher?: Intl.RelativeTimeFormatLocaleMatcher;
The locale matching algorithm to use. Other value: 'lookup'. 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)
property numeric
numeric?: Intl.RelativeTimeFormatNumeric;
The output message format. The values are 'auto' (e.g.
yesterday
), 'always'(e.g.1 day ago
)
property style
style?: Intl.RelativeTimeFormatStyle;
The length of the result. The values are: 'long' (e.g.
1 month
), 'short' (e.g. 'in 1 mo.'), 'narrow' (e.g. 'in 1 mo.'). The narrow one could be similar to the short one for some locales.
property unit
unit?: IntlFormatDistanceUnit;
Force the distance unit
interface IntlFormatLocaleOptions
interface IntlFormatLocaleOptions {}
The locale options.
property locale
locale: IntlFormatLocale | IntlFormatLocale[];
The locale(s) to use (see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#locales_argument)
interface IsMatchOptions
interface IsMatchOptions extends LocalizedOptions<'options' | 'match' | 'formatLong'>, WeekOptions, FirstWeekContainsDateOptions, AdditionalTokensOptions {}
The isMatch function options.
interface ISOFormatOptions
interface ISOFormatOptions {}
The ISO format function options. Used to build function options.
property format
format?: ISOStringFormat;
The format to use: basic with minimal number of separators or extended with separators added to enhance human readability
property representation
representation?: ISOStringRepresentation;
The date representation - what component to format: date, time\ or both (complete)
interface IsSameWeekOptions
interface IsSameWeekOptions extends WeekOptions, LocalizedOptions<'options'> {}
The isSameWeek function options.
interface IsThisWeekOptions
interface IsThisWeekOptions extends WeekOptions, LocalizedOptions<'options'> {}
The isThisWeek function options.
interface LastDayOfWeekOptions
interface LastDayOfWeekOptions extends LocalizedOptions<'options'>, WeekOptions {}
The lastDayOfWeek function options.
interface Locale
interface Locale {}
The locale object with all functions and data needed to parse and format dates. This is what each locale implements and exports.
property code
code: string;
The locale code (ISO 639-1 + optional country code)
property formatDistance
formatDistance: FormatDistanceFn;
The function to format distance
property formatLong
formatLong: FormatLong;
The object with functions that return localized formats
property formatRelative
formatRelative: FormatRelativeFn;
The function to relative time
property localize
localize: Localize;
The object with functions used to localize various values
property match
match: Match;
The object with functions used to match and parse various localized values
property options
options?: LocaleOptions;
An object with locale options
interface LocaleOptions
interface LocaleOptions extends WeekOptions, FirstWeekContainsDateOptions {}
The locale options.
interface Localize
interface Localize {}
The object with functions used to localize various values. Part of the public locale API.
property day
day: LocalizeFn<Day>;
The function that localizes the day of the week
property dayPeriod
dayPeriod: LocalizeFn<LocaleDayPeriod>;
The function that localizes the day period
property era
era: LocalizeFn<Era>;
The function that localized the era
property month
month: LocalizeFn<Month>;
The function that localizes the month
property ordinalNumber
ordinalNumber: LocalizeFn<number>;
The function that localizes an ordinal number
property preprocessor
preprocessor?: <DateType extends Date>( date: DateType, parts: FormatPart[]) => FormatPart[];
The function that can preprocess parts/tokens *
property quarter
quarter: LocalizeFn<Quarter>;
The function that localizes the quarter
interface LocalizedOptions
interface LocalizedOptions<LocaleFields extends keyof Locale> {}
The localized function options. Used to build function options.
property locale
locale?: Pick<Locale, LocaleFields>;
The locale to use in the function.
interface LocalizeFnOptions
interface LocalizeFnOptions {}
The LocalizeFn function options.
property context
context?: 'formatting' | 'standalone';
The context where the formatted value is used - standalone: the result should make grammatical sense as is and formatting: the result is a part of the formatted string. See: https://date-fns.org/docs/I18n-Contribution-Guide
property unit
unit?: LocaleUnit;
The unit to format
property width
width?: LocaleWidth;
The width to use formatting the value, defines how short or long the formatted string might be.
interface Match
interface Match {}
The object with functions used to match and parse various localized values.
property day
day: MatchFn<Day>;
The function that parses a localized day of the week.
property dayPeriod
dayPeriod: MatchFn<LocaleDayPeriod>;
The function that parses a localized time of the day.
property era
era: MatchFn<Era>;
The function that parses a localized era.
property month
month: MatchFn<Month>;
The function that parses a localized month.
property ordinalNumber
ordinalNumber: MatchFn< number, { unit: LocaleUnit; }>;
The function that parses a localized ordinal number.
property quarter
quarter: MatchFn<Quarter>;
The function that parses a localized quarter.
interface MatchFnOptions
interface MatchFnOptions<Result> {}
The MatchFn function options.
property valueCallback
valueCallback?: MatchValueCallback<string, Result>;
Example 1
const matchResult = locale.match.ordinalNumber('1st') if (matchResult) { matchResult.value = valueCallback(matchResult.value) }
Deprecated
Map the value manually instead.
property width
width?: LocaleWidth;
The width to use matching the value, defines how short or long the matched string might be.
interface MatchFnResult
interface MatchFnResult<Result> {}
The MatchFn function result.
interface NearestToUnitOptions
interface NearestToUnitOptions<Unit extends number> {}
The nearest unit function options. Used to build function options.
property nearestTo
nearestTo?: Unit;
The nearest unit to round to. E.g. for minutes
15
to round to quarter hours.
interface NormalizedInterval
interface NormalizedInterval<DateType extends Date = Date> {}
A version of Interval that has both start and end resolved to Date.
interface ParseISOOptions
interface ParseISOOptions {}
The parseISO function options.
property additionalDigits
additionalDigits?: 0 | 1 | 2;
The additional number of digits in the extended year format
interface ParseOptions
interface ParseOptions extends LocalizedOptions<'options' | 'match' | 'formatLong'>, FirstWeekContainsDateOptions, WeekOptions, AdditionalTokensOptions {}
The parse function options.
interface RoundingOptions
interface RoundingOptions {}
The rounding options. Used to build function options.
property roundingMethod
roundingMethod?: RoundingMethod;
The rounding method to use
interface RoundToNearestHoursOptions
interface RoundToNearestHoursOptions extends NearestToUnitOptions<NearestHours>, RoundingOptions {}
The roundToNearestHours function options.
interface RoundToNearestMinutesOptions
interface RoundToNearestMinutesOptions extends NearestToUnitOptions<NearestMinutes>, RoundingOptions {}
The roundToNearestMinutes function options.
interface SetDayOptions
interface SetDayOptions extends LocalizedOptions<'options'>, WeekOptions {}
The setDay function options.
interface SetWeekOptions
interface SetWeekOptions extends LocalizedOptions<'options'>, WeekOptions, FirstWeekContainsDateOptions {}
The setWeek function options.
interface SetWeekYearOptions
interface SetWeekYearOptions extends LocalizedOptions<'options'>, WeekOptions, FirstWeekContainsDateOptions {}
The setWeekYear function options.
interface StartOfWeekOptions
interface StartOfWeekOptions extends LocalizedOptions<'options'>, WeekOptions {}
The startOfWeek function options.
interface StartOfWeekYearOptions
interface StartOfWeekYearOptions extends LocalizedOptions<'options'>, FirstWeekContainsDateOptions, WeekOptions {}
The startOfWeekYear function options.
interface StepOptions
interface StepOptions {}
The step function options. Used to build function options.
property step
step?: number;
The step to use when iterating
interface WeekOptions
interface WeekOptions {}
The week function options. Used to build function options.
property weekStartsOn
weekStartsOn?: Day;
Which day the week starts on.
Type Aliases
type Day
type Day = 0 | 1 | 2 | 3 | 4 | 5 | 6;
The day of the week type alias. Unlike the date (the number of days since the beginning of the month), which begins with 1 and is dynamic (can go up to 28, 30, or 31), the day starts with 0 and static (always ends at 6). Look at it as an index in an array where Sunday is the first element and Saturday is the last.
type DurationUnit
type DurationUnit = keyof Duration;
The duration unit type alias.
type Era
type Era = 0 | 1;
The era. Can be either 0 (AD - Anno Domini) or 1 (BC - Before Christ).
type FirstWeekContainsDate
type FirstWeekContainsDate = 1 | 4;
FirstWeekContainsDate is used to determine which week is the first week of the year, based on what day the January, 1 is in that week.
The day in that week can only be 1 (Monday) or 4 (Thursday).
Please see https://en.wikipedia.org/wiki/Week#The_ISO_week_date_system for more information.
type FormatDistanceFn
type FormatDistanceFn = ( token: FormatDistanceToken, count: number, options?: FormatDistanceFnOptions) => string;
The function that takes a token (i.e. halfAMinute) passed by
formatDistance
orformatDistanceStrict
and payload, and returns localized distance.Parameter token
The token to localize
Parameter count
The distance number
Parameter options
The object with options
Returns
The localized distance in words
type FormatDistanceLocale
type FormatDistanceLocale<Template> = { [Token in FormatDistanceToken]: Template;};
The tokens map to string templates used in the format distance function. It looks like this:
const formatDistanceLocale: FormatDistanceLocale = { lessThanXSeconds: 'តិចជាង {{count}} វិនាទី', xSeconds: '{{count}} វិនាទី', // ... }
type FormatDistanceStrictUnit
type FormatDistanceStrictUnit = | 'second' | 'minute' | 'hour' | 'day' | 'month' | 'year';
The unit used to format the distance in formatDistanceStrict.
type FormatDistanceToken
type FormatDistanceToken = | 'lessThanXSeconds' | 'xSeconds' | 'halfAMinute' | 'lessThanXMinutes' | 'xMinutes' | 'aboutXHours' | 'xHours' | 'xDays' | 'aboutXWeeks' | 'xWeeks' | 'aboutXMonths' | 'xMonths' | 'aboutXYears' | 'xYears' | 'overXYears' | 'almostXYears';
The token used in the format distance function. Represents the distance unit with prespecified precision.
type FormatDistanceTokenFn
type FormatDistanceTokenFn = ( /** The distance as number to format */ count: number, /** The object with options */ options?: FormatDistanceFnOptions) => string;
The function used inside the FormatDistanceFn function, implementing formatting for a particular token.
type FormatLongFn
type FormatLongFn = (options: FormatLongFnOptions) => string;
The format long function. Formats date, time or both.
Parameter options
The object with options
Returns
The localized string
type FormatLongWidth
type FormatLongWidth = 'full' | 'long' | 'medium' | 'short' | 'any';
The format long width token, defines how short or long the formnatted value might be. The actual result length is defined by the locale.
type FormatRelativeFn
type FormatRelativeFn = <DateType extends Date>( token: FormatRelativeToken, date: DateType, baseDate: DateType, options?: FormatRelativeFnOptions) => string;
The locale function that does the work for the
formatRelative
function.Parameter token
The token to localize
Parameter date
The date to format
Parameter baseDate
The date to compare with
Parameter options
The object with options
Returns
The localized relative date format
type FormatRelativeToken
type FormatRelativeToken = | 'lastWeek' | 'yesterday' | 'today' | 'tomorrow' | 'nextWeek' | 'other';
The token used in format relative function. Represents the time unit.
type FormatRelativeTokenFn
type FormatRelativeTokenFn = <DateType extends Date>( date: DateType | number | string, baseDate: DateType | number | string, options?: FormatRelativeTokenFnOptions) => string;
The locale function used inside the FormatRelativeFn function implementing formatting for a particular token.
Parameter date
The date to format
Parameter baseDate
The date to compare with
Parameter options
The object with options
type FPArity
type FPArity = 1 | 2 | 3 | 4;
The supported arity type.
type FPFn
type FPFn<Fn extends FPFnInput, Arity extends FPArity> = Arity extends 4 ? FPFn4< ReturnType<Fn>, Parameters<Fn>[3], Parameters<Fn>[2], Parameters<Fn>[1], Parameters<Fn>[0] > : Arity extends 3 ? FPFn3<ReturnType<Fn>, Parameters<Fn>[2], Parameters<Fn>[1], Parameters<Fn>[0]> : Arity extends 2 ? FPFn2<ReturnType<Fn>, Parameters<Fn>[1], Parameters<Fn>[0]> : Arity extends 1 ? FPFn1<ReturnType<Fn>, Parameters<Fn>[0]> : never;
FP function interface. It infers the arity of the function and returns the corresponding FP function interface.
type FPFnInput
type FPFnInput = (...args: any[]) => any;
The type of a function that can be converted to FP.
type IntlFormatDistanceUnit
type IntlFormatDistanceUnit = | 'year' | 'quarter' | 'month' | 'week' | 'day' | 'hour' | 'minute' | 'second';
The unit used to format the distance in intlFormatDistance.
type IntlFormatFormatOptions
type IntlFormatFormatOptions = Intl.DateTimeFormatOptions;
The format options (see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat#options)
type IntlFormatLocale
type IntlFormatLocale = Intl.ResolvedDateTimeFormatOptions['locale'];
The locale string (see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#locales_argument).
type ISOStringFormat
type ISOStringFormat = 'extended' | 'basic';
The ISO string format.
- basic: Minimal number of separators - extended: With separators added to enhance human readability
type ISOStringRepresentation
type ISOStringRepresentation = 'complete' | 'date' | 'time';
The ISO date representation. Represents which component the string includes, date, time or both.
type LocaleDayPeriod
type LocaleDayPeriod = | 'am' | 'pm' | 'midnight' | 'noon' | 'morning' | 'afternoon' | 'evening' | 'night';
Token representing particular period of the day.
type LocaleUnit
type LocaleUnit = | 'second' | 'minute' | 'hour' | 'day' | 'dayOfYear' | 'date' | 'week' | 'month' | 'quarter' | 'year';
The units commonly used in the date formatting or parsing.
type LocaleUnitValue
type LocaleUnitValue = Era | Quarter | Month | Day | LocaleDayPeriod;
The formatting unit value, represents the raw value that can be formatted.
type LocaleWidth
type LocaleWidth = 'narrow' | 'short' | 'abbreviated' | 'wide' | 'any';
The format width. Defines how short or long the formatted string might be. The actaul result length depends on the locale.
type LocalizeFn
type LocalizeFn<Value extends LocaleUnitValue | number> = ( value: Value, options?: LocalizeFnOptions) => string;
Individual localize function. Part of Localize.
Parameter value
The value to localize
Parameter options
The object with options
Returns
The localized string
type MatchFn
type MatchFn<Result, ExtraOptions = Record<string, unknown>> = ( str: string, options?: MatchFnOptions<Result> & ExtraOptions) => MatchFnResult<Result> | null;
The match function. Part of Match. Implements matcher for particular unit type.
Parameter str
The string to match
Parameter options
The object with options
Returns
The match result or null if match failed
type MatchValueCallback
type MatchValueCallback<Arg, Result> = (value: Arg) => Result;
The function that allows to map the matched value to the actual type.
Parameter arg
The value to match
Returns
The matched value
type Month
type Month = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11;
The month type alias. Goes from 0 to 11, where 0 is January and 11 is December.
type NearestHours
type NearestHours = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12;
Nearest hour type. Goes from 1 to 12, where 1 is the nearest hour and 12 is nearest half a day.
type NearestMinutes
type NearestMinutes = | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30;
Nearest minute type. Goes from 1 to 30, where 1 is the nearest minute and 30 is nearest half an hour.
type NearestMinutesOptions
type NearestMinutesOptions = NearestToUnitOptions<NearestMinutes>;
The nearest minutes function options. Used to build function options.
Deprecated
Use NearestToUnitOptions instead.
type Quarter
type Quarter = 1 | 2 | 3 | 4;
The year quarter. Goes from 1 to 4.
type RoundingMethod
type RoundingMethod = 'ceil' | 'floor' | 'round' | 'trunc';
The number rounding method.
Package Files (253)
- _lib/format/formatters.d.ts
- _lib/format/lightFormatters.d.ts
- _lib/format/longFormatters.d.ts
- add.d.ts
- addBusinessDays.d.ts
- addDays.d.ts
- addHours.d.ts
- addISOWeekYears.d.ts
- addMilliseconds.d.ts
- addMinutes.d.ts
- addMonths.d.ts
- addQuarters.d.ts
- addSeconds.d.ts
- addWeeks.d.ts
- addYears.d.ts
- areIntervalsOverlapping.d.ts
- clamp.d.ts
- closestIndexTo.d.ts
- closestTo.d.ts
- compareAsc.d.ts
- compareDesc.d.ts
- constructFrom.d.ts
- constructNow.d.ts
- daysToWeeks.d.ts
- differenceInBusinessDays.d.ts
- differenceInCalendarDays.d.ts
- differenceInCalendarISOWeekYears.d.ts
- differenceInCalendarISOWeeks.d.ts
- differenceInCalendarMonths.d.ts
- differenceInCalendarQuarters.d.ts
- differenceInCalendarWeeks.d.ts
- differenceInCalendarYears.d.ts
- differenceInDays.d.ts
- differenceInHours.d.ts
- differenceInISOWeekYears.d.ts
- differenceInMilliseconds.d.ts
- differenceInMinutes.d.ts
- differenceInMonths.d.ts
- differenceInQuarters.d.ts
- differenceInSeconds.d.ts
- differenceInWeeks.d.ts
- differenceInYears.d.ts
- eachDayOfInterval.d.ts
- eachHourOfInterval.d.ts
- eachMinuteOfInterval.d.ts
- eachMonthOfInterval.d.ts
- eachQuarterOfInterval.d.ts
- eachWeekOfInterval.d.ts
- eachWeekendOfInterval.d.ts
- eachWeekendOfMonth.d.ts
- eachWeekendOfYear.d.ts
- eachYearOfInterval.d.ts
- endOfDay.d.ts
- endOfDecade.d.ts
- endOfHour.d.ts
- endOfISOWeek.d.ts
- endOfISOWeekYear.d.ts
- endOfMinute.d.ts
- endOfMonth.d.ts
- endOfQuarter.d.ts
- endOfSecond.d.ts
- endOfToday.d.ts
- endOfTomorrow.d.ts
- endOfWeek.d.ts
- endOfYear.d.ts
- endOfYesterday.d.ts
- format.d.ts
- formatDistance.d.ts
- formatDistanceStrict.d.ts
- formatDistanceToNow.d.ts
- formatDistanceToNowStrict.d.ts
- formatDuration.d.ts
- formatISO.d.ts
- formatISO9075.d.ts
- formatISODuration.d.ts
- formatRFC3339.d.ts
- formatRFC7231.d.ts
- formatRelative.d.ts
- fp/types.d.ts
- fromUnixTime.d.ts
- getDate.d.ts
- getDay.d.ts
- getDayOfYear.d.ts
- getDaysInMonth.d.ts
- getDaysInYear.d.ts
- getDecade.d.ts
- getDefaultOptions.d.ts
- getHours.d.ts
- getISODay.d.ts
- getISOWeek.d.ts
- getISOWeekYear.d.ts
- getISOWeeksInYear.d.ts
- getMilliseconds.d.ts
- getMinutes.d.ts
- getMonth.d.ts
- getOverlappingDaysInIntervals.d.ts
- getQuarter.d.ts
- getSeconds.d.ts
- getTime.d.ts
- getUnixTime.d.ts
- getWeek.d.ts
- getWeekOfMonth.d.ts
- getWeekYear.d.ts
- getWeeksInMonth.d.ts
- getYear.d.ts
- hoursToMilliseconds.d.ts
- hoursToMinutes.d.ts
- hoursToSeconds.d.ts
- index.d.ts
- interval.d.ts
- intervalToDuration.d.ts
- intlFormat.d.ts
- intlFormatDistance.d.ts
- isAfter.d.ts
- isBefore.d.ts
- isDate.d.ts
- isEqual.d.ts
- isExists.d.ts
- isFirstDayOfMonth.d.ts
- isFriday.d.ts
- isFuture.d.ts
- isLastDayOfMonth.d.ts
- isLeapYear.d.ts
- isMatch.d.ts
- isMonday.d.ts
- isPast.d.ts
- isSameDay.d.ts
- isSameHour.d.ts
- isSameISOWeek.d.ts
- isSameISOWeekYear.d.ts
- isSameMinute.d.ts
- isSameMonth.d.ts
- isSameQuarter.d.ts
- isSameSecond.d.ts
- isSameWeek.d.ts
- isSameYear.d.ts
- isSaturday.d.ts
- isSunday.d.ts
- isThisHour.d.ts
- isThisISOWeek.d.ts
- isThisMinute.d.ts
- isThisMonth.d.ts
- isThisQuarter.d.ts
- isThisSecond.d.ts
- isThisWeek.d.ts
- isThisYear.d.ts
- isThursday.d.ts
- isToday.d.ts
- isTomorrow.d.ts
- isTuesday.d.ts
- isValid.d.ts
- isWednesday.d.ts
- isWeekend.d.ts
- isWithinInterval.d.ts
- isYesterday.d.ts
- lastDayOfDecade.d.ts
- lastDayOfISOWeek.d.ts
- lastDayOfISOWeekYear.d.ts
- lastDayOfMonth.d.ts
- lastDayOfQuarter.d.ts
- lastDayOfWeek.d.ts
- lastDayOfYear.d.ts
- lightFormat.d.ts
- locale/types.d.ts
- max.d.ts
- milliseconds.d.ts
- millisecondsToHours.d.ts
- millisecondsToMinutes.d.ts
- millisecondsToSeconds.d.ts
- min.d.ts
- minutesToHours.d.ts
- minutesToMilliseconds.d.ts
- minutesToSeconds.d.ts
- monthsToQuarters.d.ts
- monthsToYears.d.ts
- nextDay.d.ts
- nextFriday.d.ts
- nextMonday.d.ts
- nextSaturday.d.ts
- nextSunday.d.ts
- nextThursday.d.ts
- nextTuesday.d.ts
- nextWednesday.d.ts
- parse.d.ts
- parse/_lib/parsers.d.ts
- parseISO.d.ts
- parseJSON.d.ts
- previousDay.d.ts
- previousFriday.d.ts
- previousMonday.d.ts
- previousSaturday.d.ts
- previousSunday.d.ts
- previousThursday.d.ts
- previousTuesday.d.ts
- previousWednesday.d.ts
- quartersToMonths.d.ts
- quartersToYears.d.ts
- roundToNearestHours.d.ts
- roundToNearestMinutes.d.ts
- secondsToHours.d.ts
- secondsToMilliseconds.d.ts
- secondsToMinutes.d.ts
- set.d.ts
- setDate.d.ts
- setDay.d.ts
- setDayOfYear.d.ts
- setDefaultOptions.d.ts
- setHours.d.ts
- setISODay.d.ts
- setISOWeek.d.ts
- setISOWeekYear.d.ts
- setMilliseconds.d.ts
- setMinutes.d.ts
- setMonth.d.ts
- setQuarter.d.ts
- setSeconds.d.ts
- setWeek.d.ts
- setWeekYear.d.ts
- setYear.d.ts
- startOfDay.d.ts
- startOfDecade.d.ts
- startOfHour.d.ts
- startOfISOWeek.d.ts
- startOfISOWeekYear.d.ts
- startOfMinute.d.ts
- startOfMonth.d.ts
- startOfQuarter.d.ts
- startOfSecond.d.ts
- startOfToday.d.ts
- startOfTomorrow.d.ts
- startOfWeek.d.ts
- startOfWeekYear.d.ts
- startOfYear.d.ts
- startOfYesterday.d.ts
- sub.d.ts
- subBusinessDays.d.ts
- subDays.d.ts
- subHours.d.ts
- subISOWeekYears.d.ts
- subMilliseconds.d.ts
- subMinutes.d.ts
- subMonths.d.ts
- subQuarters.d.ts
- subSeconds.d.ts
- subWeeks.d.ts
- subYears.d.ts
- toDate.d.ts
- transpose.d.ts
- types.d.ts
- weeksToDays.d.ts
- yearsToDays.d.ts
- yearsToMonths.d.ts
- yearsToQuarters.d.ts
Dependencies (0)
No dependencies.
Dev Dependencies (37)
- @babel/cli
- @babel/core
- @babel/preset-env
- @babel/preset-typescript
- @date-fns/docs
- @date-fns/utc
- @octokit/core
- @size-limit/esbuild
- @size-limit/file
- @types/bun
- @types/lodash
- @types/node
- @types/sinon
- @typescript-eslint/eslint-plugin
- @typescript-eslint/parser
- @vitest/browser
- @vitest/coverage-v8
- babel-plugin-add-import-extension
- bun
- cloc
- coveralls
- eslint
- firebase
- fp-ts
- js-fns
- jscodeshift
- lodash
- playwright
- prettier
- simple-git
- sinon
- size-limit
- tsx
- typedoc
- typedoc-plugin-missing-exports
- typescript
- vitest
Peer Dependencies (0)
No peer dependencies.
Badge
To add a badge like this oneto your package's README, use the codes available below.
You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/date-fns
.
- Markdown[![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/date-fns)
- HTML<a href="https://www.jsdocs.io/package/date-fns"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>
- Updated .
Package analyzed in 12711 ms. - Missing or incorrect documentation? Open an issue for this package.