Changelog
[2.0.1] - 2019-08-23
getWeekOfMonth
with options.weekStartsOn
set to 1 not working for Sundays. Kudos to @waseemahmad31!Changelog
[2.0.0] - 2019-08-20
If you're upgrading from v2 alpha or beta, see the pre-release changelog.
Fixed the toDate
bug occurring when parsing ISO-8601 style dates (but not valid ISO format)
with a trailing Z (e.g 2012-01Z
), it returned Invalid Date for FireFox/IE11 #510
Fixed differenceIn...
functions returning negative zero in some cases:
#692
isDate
now works properly with dates passed across iframes #754.
Fixed a few bugs that appeared in timezones with offsets that include seconds (e.g. GMT+00:57:44). See PR #789.
Fixed DST issue. See #972 and #992 for more details.
Fixed DST issue in eachDayOfInterval
that caused time in the days
after DST change to have the shift as well.
Fixed bug in Galician locale caused by incorrect usage of getHours
instead of getUTCHours
.
BREAKING: now functions don't accept string arguments, but only
numbers or dates. When a string is passed, it will result in
an unexpected result (Invalid Date
, NaN
, etc).
From now on a string should be parsed using parseISO
(ISO 8601)
or parse
.
In v1 we've used new Date()
to parse strings, but it resulted in many
hard-to-track bugs caused by inconsistencies in different browsers.
To address that we've implemented our ISO 8601 parser but that made
library to significantly grow in size. To prevent inevitable bugs
and keep the library tiny, we made this trade-off.
See this post for more details.
// Before v2.0.0
addDays("2016-01-01", 1);
// v2.0.0 onward
addDays(parseISO("2016-01-01"), 1);
BREAKING: new format string API for format
function
which is based on Unicode Technical Standard #35.
See this post for more details.
| Unit | v2 Pattern | v1 Pattern | Result examples | | ------------------------------- | ---------- | ---------- | --------------------------------- | | Era | G..GGG | | AD, BC | | | GGGG | | Anno Domini, Before Christ | | | GGGGG | | A, B | | Calendar year | y | | 44, 1, 1900, 2017 | | | yo | | 44th, 1st, 0th, 17th | | | yy | YY | 44, 01, 00, 17 | | | yyy | | 044, 001, 1900, 2017 | | | yyyy | YYYY | 0044, 0001, 1900, 2017 | | | yyyyy | | ... | | Local week-numbering year | Y | | 44, 1, 1900, 2017 | | | Yo | | 44th, 1st, 1900th, 2017th | | | YY | | 44, 01, 00, 17 | | | YYY | | 044, 001, 1900, 2017 | | | YYYY | | 0044, 0001, 1900, 2017 | | | YYYYY | | ... | | ISO week-numbering year | R | | -43, 0, 1, 1900, 2017 | | | RR | GG | -43, 00, 01, 1900, 2017 | | | RRR | | -043, 000, 001, 1900, 2017 | | | RRRR | GGGG | -0043, 0000, 0001, 1900, 2017 | | | RRRRR | | ... | | Extended year | u | | -43, 0, 1, 1900, 2017 | | | uu | | -43, 01, 1900, 2017 | | | uuu | | -043, 001, 1900, 2017 | | | uuuu | | -0043, 0001, 1900, 2017 | | | uuuuu | | ... | | Quarter (formatting) | Q | | 1, 2, 3, 4 | | | Qo | | 1st, 2nd, 3rd, 4th | | | QQ | | 01, 02, 03, 04 | | | QQQ | | Q1, Q2, Q3, Q4 | | | QQQQ | | 1st quarter, 2nd quarter, ... | | | QQQQQ | | 1, 2, 3, 4 | | Quarter (stand-alone) | q | Q | 1, 2, 3, 4 | | | qo | Qo | 1st, 2nd, 3rd, 4th | | | qq | | 01, 02, 03, 04 | | | qqq | | Q1, Q2, Q3, Q4 | | | qqqq | | 1st quarter, 2nd quarter, ... | | | qqqqq | | 1, 2, 3, 4 | | Month (formatting) | M | | 1, 2, ..., 12 | | | Mo | | 1st, 2nd, ..., 12th | | | MM | | 01, 02, ..., 12 | | | MMM | | Jan, Feb, ..., Dec | | | MMMM | | January, February, ..., December | | | MMMMM | | J, F, ..., D | | Month (stand-alone) | L | M | 1, 2, ..., 12 | | | Lo | | 1st, 2nd, ..., 12th | | | LL | MM | 01, 02, ..., 12 | | | LLL | MMM | Jan, Feb, ..., Dec | | | LLLL | MMMM | January, February, ..., December | | | LLLLL | | J, F, ..., D | | Local week of year | w | | 1, 2, ..., 53 | | | wo | | 1st, 2nd, ..., 53th | | | ww | | 01, 02, ..., 53 | | ISO week of year | I | W | 1, 2, ..., 53 | | | Io | Wo | 1st, 2nd, ..., 53th | | | II | WW | 01, 02, ..., 53 | | Day of month | d | D | 1, 2, ..., 31 | | | do | Do | 1st, 2nd, ..., 31st | | | dd | DD | 01, 02, ..., 31 | | Day of year | D | DDD | 1, 2, ..., 365, 366 | | | Do | DDDo | 1st, 2nd, ..., 365th, 366th | | | DD | | 01, 02, ..., 365, 366 | | | DDD | DDDD | 001, 002, ..., 365, 366 | | | DDDD | | ... | | Day of week (formatting) | E..EEE | | Mon, Tue, Wed, ..., Su | | | EEEE | | Monday, Tuesday, ..., Sunday | | | EEEEE | | M, T, W, T, F, S, S | | | EEEEEE | | Mo, Tu, We, Th, Fr, Sa, Su | | ISO day of week (formatting) | i | E | 1, 2, 3, ..., 7 | | | io | do | 1st, 2nd, ..., 7th | | | ii | | 01, 02, ..., 07 | | | iii | ddd | Mon, Tue, Wed, ..., Su | | | iiii | dddd | Monday, Tuesday, ..., Sunday | | | iiiii | | M, T, W, T, F, S, S | | | iiiiii | dd | Mo, Tu, We, Th, Fr, Sa, Su | | Local day of week (formatting) | e | | 2, 3, 4, ..., 1 | | | eo | | 2nd, 3rd, ..., 1st | | | ee | | 02, 03, ..., 01 | | | eee | | Mon, Tue, Wed, ..., Su | | | eeee | | Monday, Tuesday, ..., Sunday | | | 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 | | | cc | | 02, 03, ..., 01 | | | ccc | | Mon, Tue, Wed, ..., Su | | | cccc | | Monday, Tuesday, ..., Sunday | | | ccccc | | M, T, W, T, F, S, S | | | cccccc | | Mo, Tu, We, Th, Fr, Sa, Su | | AM, PM | a..aaa | A | AM, PM | | | aaaa | aa | a.m., p.m. | | | aaaaa | | a, p | | AM, PM, noon, midnight | b..bbb | | AM, PM, noon, midnight | | | bbbb | | a.m., p.m., noon, midnight | | | bbbbb | | a, p, n, mi | | Flexible day period | B..BBB | | at night, in the morning, ... | | | BBBB | | at night, in the morning, ... | | | BBBBB | | at night, in the morning, ... | | Hour [1-12] | h | | 1, 2, ..., 11, 12 | | | ho | | 1st, 2nd, ..., 11th, 12th | | | hh | | 01, 02, ..., 11, 12 | | Hour [0-23] | H | | 0, 1, 2, ..., 23 | | | Ho | | 0th, 1st, 2nd, ..., 23rd | | | HH | | 00, 01, 02, ..., 23 | | Hour [0-11] | K | | 1, 2, ..., 11, 0 | | | Ko | | 1st, 2nd, ..., 11th, 0th | | | KK | | 1, 2, ..., 11, 0 | | Hour [1-24] | k | | 24, 1, 2, ..., 23 | | | ko | | 24th, 1st, 2nd, ..., 23rd | | | kk | | 24, 01, 02, ..., 23 | | Minute | m | | 0, 1, ..., 59 | | | mo | | 0th, 1st, ..., 59th | | | mm | | 00, 01, ..., 59 | | Second | s | | 0, 1, ..., 59 | | | so | | 0th, 1st, ..., 59th | | | ss | | 00, 01, ..., 59 | | Fraction of second | S | | 0, 1, ..., 9 | | | SS | | 00, 01, ..., 99 | | | SSS | | 000, 0001, ..., 999 | | | SSSS | | ... | | Timezone (ISO-8601 w/ Z) | X | | -08, +0530, Z | | | XX | | -0800, +0530, Z | | | XXX | | -08:00, +05:30, Z | | | XXXX | | -0800, +0530, Z, +123456 | | | XXXXX | | -08:00, +05:30, Z, +12:34:56 | | Timezone (ISO-8601 w/o Z) | x | | -08, +0530, +00 | | | xx | ZZ | -0800, +0530, +0000 | | | xxx | Z | -08:00, +05:30, +00:00 | | | 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 | | Timezone (specific non-locat.) | z...zzz | | GMT-8, GMT+5:30, GMT+0 | | | zzzz | | GMT-08:00, GMT+05:30, GMT+00:00 | | Seconds timestamp | t | X | 512969520 | | | tt | | ... | | Milliseconds timestamp | T | x | 512969520900 | | | TT | | ... | | Long localized date | P | | 5/29/53 | | | PP | | May 29, 1453 | | | PPP | | May 29th, 1453 | | | PPPP | | Sunday, May 29th, 1453 | | Long localized time | p | | 12:00 AM | | | pp | | 12:00:00 AM | | | ppp | | 12:00:00 AM GMT+2 | | | pppp | | 12:00:00 AM GMT+02:00 | | Combination of date and time | Pp | | 5/29/53, 12:00 AM | | | PPpp | | May 29, 1453, 12:00 AM | | | PPPppp | | May 29th, 1453 at ... | | | PPPPpppp | | Sunday, May 29th, 1453 at ... |
Characters are now escaped using single quote symbols ('
) instead of square brackets.
format
now throws RangeError if it encounters an unescaped latin character
that isn't a valid formatting token.
To use YY
and YYYY
tokens that represent week-numbering years,
you should set useAdditionalWeekYearTokens
option:
format(Date.now(), "YY", { useAdditionalWeekYearTokens: true });
//=> '86'
To use D
and DD
tokens which represent days of the year,
set useAdditionalDayOfYearTokens
option:
format(Date.now(), "D", { useAdditionalDayOfYearTokens: true });
//=> '364'
BREAKING: function submodules now use camelCase naming schema:
// Before v2.0.0
import differenceInCalendarISOYears from "date-fns/difference_in_calendar_iso_years";
// v2.0.0 onward
import differenceInCalendarISOYears from "date-fns/differenceInCalendarISOYears";
BREAKING: min and max functions now accept an array of dates rather than spread arguments.
// Before v2.0.0
var date1 = new Date(1989, 6 /* Jul */, 10);
var date2 = new Date(1987, 1 /* Feb */, 11);
var minDate = min(date1, date2);
var maxDate = max(date1, date2);
// v2.0.0 onward:
var dates = [
new Date(1989, 6 /* Jul */, 10),
new Date(1987, 1 /* Feb */, 11),
];
var minDate = min(dates);
var maxDate = max(dates);
BREAKING: make the second argument of format
required for the sake of explicitness.
// Before v2.0.0
format(new Date(2016, 0, 1));
// v2.0.0 onward
format(new Date(2016, 0, 1), "yyyy-MM-dd'T'HH:mm:ss.SSSxxx");
BREAKING renamed ISO week-numbering year helpers:
addISOYears
→ addISOWeekYears
differenceInCalendarISOYears
→ differenceInCalendarISOWeekYears
differenceInISOYears
→ differenceInISOWeekYears
endOfISOYear
→ endOfISOWeekYear
getISOYear
→ getISOWeekYear
isSameISOYear
→ isSameISOWeekYear
lastDayOfISOYear
→ lastDayOfISOWeekYear
setISOYear
→ setISOWeekYear
subISOYears
→ subISOWeekYears
i.e. "ISO year" renamed to "ISO week year", which is short for
ISO week-numbering year.
It makes them consistent with locale-dependent week-numbering year helpers,
e.g., startOfWeekYear
.
BREAKING: functions renamed:
areRangesOverlapping
→ areIntervalsOverlapping
eachDay
→ eachDayOfInterval
getOverlappingDaysInRanges
→ getOverlappingDaysInIntervals
isWithinRange
→ isWithinInterval
This change was made to mirror the use of the word "interval" in standard ISO 8601:2004 terminology:
2.1.3
time interval
part of the time axis limited by two instants
Also these functions now accept an object with start
and end
properties
instead of two arguments as an interval. All these functions
throw RangeError
if the start of the interval is after its end
or if any date in the interval is Invalid Date
.
// Before v2.0.0
areRangesOverlapping(
new Date(2014, 0, 10),
new Date(2014, 0, 20),
new Date(2014, 0, 17),
new Date(2014, 0, 21),
);
eachDay(new Date(2014, 0, 10), new Date(2014, 0, 20));
getOverlappingDaysInRanges(
new Date(2014, 0, 10),
new Date(2014, 0, 20),
new Date(2014, 0, 17),
new Date(2014, 0, 21),
);
isWithinRange(
new Date(2014, 0, 3),
new Date(2014, 0, 1),
new Date(2014, 0, 7),
);
// v2.0.0 onward
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) },
);
eachDayOfInterval({
start: new Date(2014, 0, 10),
end: new Date(2014, 0, 20),
});
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) },
);
isWithinInterval(new Date(2014, 0, 3), {
start: new Date(2014, 0, 1),
end: new Date(2014, 0, 7),
});
BREAKING: functions renamed:
distanceInWords
→ formatDistance
distanceInWordsStrict
→ formatDistanceStrict
distanceInWordsToNow
→ formatDistanceToNow
to make them consistent with format
and formatRelative
.
BREAKING: The order of arguments of distanceInWords
and distanceInWordsStrict
is swapped to make them consistent with differenceIn...
functions.
// Before v2.0.0
distanceInWords(
new Date(1986, 3, 4, 10, 32, 0),
new Date(1986, 3, 4, 11, 32, 0),
{ addSuffix: true },
); //=> 'in about 1 hour'
// v2.0.0 onward
formatDistance(
new Date(1986, 3, 4, 11, 32, 0),
new Date(1986, 3, 4, 10, 32, 0),
{ addSuffix: true },
); //=> 'in about 1 hour'
BREAKING: partialMethod
option in formatDistanceStrict
is renamed to roundingMethod
.
// Before v2.0.0
distanceInWordsStrict(
new Date(1986, 3, 4, 10, 32, 0),
new Date(1986, 3, 4, 10, 33, 1),
{ partialMethod: "ceil" },
); //=> '2 minutes'
// v2.0.0 onward
formatDistanceStrict(
new Date(1986, 3, 4, 10, 33, 1),
new Date(1986, 3, 4, 10, 32, 0),
{ roundingMethod: "ceil" },
); //=> '2 minutes'
BREAKING: in formatDistanceStrict
, if roundingMethod
is not specified,
it now defaults to round
instead of floor
.
BREAKING: unit
option in formatDistanceStrict
now accepts one of the strings:
'second', 'minute', 'hour', 'day', 'month' or 'year' instead of 's', 'm', 'h', 'd', 'M' or 'Y'
// Before v2.0.0
distanceInWordsStrict(
new Date(1986, 3, 4, 10, 32, 0),
new Date(1986, 3, 4, 10, 33, 1),
{ unit: "m" },
);
// v2.0.0 onward
formatDistanceStrict(
new Date(1986, 3, 4, 10, 33, 1),
new Date(1986, 3, 4, 10, 32, 0),
{ unit: "minute" },
);
BREAKING: parse
that previously used to convert strings and
numbers to dates now parses only strings in an arbitrary format
specified as an argument. Use toDate
to coerce numbers and parseISO
to parse ISO 8601 strings.
// Before v2.0.0
parse("2016-01-01");
parse(1547005581366);
parse(new Date()); // Clone the date
// v2.0.0 onward
parse("2016-01-01", "yyyy-MM-dd", new Date());
parseISO("2016-01-01");
toDate(1547005581366);
toDate(new Date()); // Clone the date
BREAKING: toDate
(previously parse
) now doesn't accept string
arguments but only numbers and dates. toDate
called with an invalid
argument will return Invalid Date
.
BREAKING: new locale format. See docs/Locale. Locales renamed:
en
→ en-US
zh_cn
→ zh-CN
zh_tw
→ zh-TW
// Before v2.0.0
import locale from "date-fns/locale/zh_cn";
// v2.0.0 onward
import locale from "date-fns/locale/zh-CN";
BREAKING: now closestTo
and closestIndexTo
don't throw an exception
when the second argument is not an array, and return Invalid Date instead.
BREAKING: now isValid
doesn't throw an exception
if the first argument is not an instance of Date.
Instead, argument is converted beforehand using toDate
.
Examples:
| isValid
argument | Before v2.0.0 | v2.0.0 onward |
| ------------------------- | ------------- | ------------- |
| new Date()
| true
| true
|
| new Date('2016-01-01')
| true
| true
|
| new Date('')
| false
| false
|
| new Date(1488370835081)
| true
| true
|
| new Date(NaN)
| false
| false
|
| '2016-01-01'
| TypeError
| false
|
| ''
| TypeError
| false
|
| 1488370835081
| TypeError
| true
|
| NaN
| TypeError
| false
|
We introduce this change to make date-fns consistent with ECMAScript behavior that try to coerce arguments to the expected type (which is also the case with other date-fns functions).
BREAKING: functions now throw RangeError
if optional values passed to options
are not undefined
or have expected values.
This change is introduced for consistency with ECMAScript standard library which does the same.
BREAKING: format
, formatDistance
(previously distanceInWords
) and
formatDistanceStrict
(previously distanceInWordsStrict
) now throw
RangeError
if one of the passed arguments is invalid. It reflects behavior of
toISOString
and Intl API. See #1032.
BREAKING: all functions now implicitly convert arguments by following rules:
| | date | number | string | boolean | | --------- | ------------ | ------ | ----------- | ------- | | 0 | new Date(0) | 0 | '0' | false | | '0' | Invalid Date | 0 | '0' | false | | 1 | new Date(1) | 1 | '1' | true | | '1' | Invalid Date | 1 | '1' | true | | true | Invalid Date | NaN | 'true' | true | | false | Invalid Date | NaN | 'false' | false | | null | Invalid Date | NaN | 'null' | false | | undefined | Invalid Date | NaN | 'undefined' | false | | NaN | Invalid Date | NaN | 'NaN' | false |
Notes:
Date
are converted to Date
using date-fns' toDate
function;toInteger
implementation
(see #765);String
function;Boolean
function.null
and undefined
passed to optional arguments (i.e. properties of options
argument)
are ignored as if no argument was passed.
If any resulting argument is invalid (i.e. NaN
for numbers and Invalid Date
for dates),
an invalid value will be returned:
false
for functions that return booleans (expect isValid
);Invalid Date
for functions that return dates;NaN
for functions that return numbers.BREAKING: all functions now check if the passed number of arguments is less
than the number of required arguments and will throw TypeError
exception if so.
BREAKING: all functions that accept numbers as arguments, now coerce
values using Number()
and also round off decimals. Positive decimals are
rounded using Math.floor
, decimals less than zero are rounded using
Math.ceil
.
BREAKING: The Bower & UMD/CDN package versions are no longer supported.
BREAKING: null
now is not a valid date. isValid(null)
returns false
;
toDate(null)
returns an invalid date. Since toDate
is used internally
by all the functions, operations over null
will also return an invalid date.
See #537 for the reasoning.
toDate
(previously parse
) and isValid
functions now accept any
type
as the first argument.
Exclude docs.json
from the npm package. Kudos to @hawkrives.
FP functions like those in lodash, that support currying, and, as a consequence, functional-style function composing.
Functions with options (format
, parse
, etc.) have two FP counterparts:
one that has the options object as its first argument and one that hasn't.
The name of the former has WithOptions
added to the end of its name.
In FP functions, the order of arguments is reversed.
See FP Guide for more information.
import addYears from "date-fns/fp/addYears";
import formatWithOptions from "date-fns/fp/formatWithOptions";
import eo from "date-fns/locale/eo";
// If FP function has not received enough arguments, it returns another function
const addFiveYears = addYears(5);
// Several arguments can be curried at once
const dateToString = formatWithOptions({ locale: eo }, "d MMMM yyyy");
const dates = [
new Date(2017, 0 /* Jan */, 1),
new Date(2017, 1 /* Feb */, 11),
new Date(2017, 6 /* Jul */, 2),
];
const formattedDates = dates.map((date) => dateToString(addFiveYears(date)));
//=> ['1 januaro 2022', '11 februaro 2022', '2 julio 2022']
Added support for ECMAScript Modules.
It allows usage with bundlers that support tree-shaking, like rollup.js and webpack:
// Without tree-shaking:
import format from "date-fns/format";
import parse from "date-fns/parse";
// With tree-shaking:
import { format, parse } from "date-fns";
Also, ESM functions provide default export, they can be used with TypeScript to import functions in more idiomatic way:
// Before
import * as format from "date-fns/format";
// Now
import format from "date-fns/format";
formatRelative
function. See formatRelative
Flow typings for index.js
, fp/index.js
, locale/index.js
, and their ESM equivalents.
See PR #558
New locale-dependent week-numbering year helpers:
getWeek
getWeekYear
setWeek
setWeekYear
startOfWeekYear
Added eachWeekOfInterval
, the weekly equivalent of eachDayOfInterval
Added getUnixTime
function. Kudos to @Kingwl.
New decade helpers. Thanks to @y-nk!
getDecade
startOfDecade
endOfDecade
lastDayOfDecade
New roundToNearestMinutes
function. Kudos to @xkizer.
Added new function fromUnixTime
. Thanks to @xkizer.
New interval, month, and year helpers to fetch a list of all Saturdays and Sundays (weekends) for a given date interval. eachWeekendOfInterval
is the handler function while the other two are wrapper functions. Kudos to @laekettavong!
eachWeekendOfInterval
eachWeekendOfMonth
eachWeekendOfYear
Build-efficient lightFormat
that only supports the popular subset of tokens. See #1050.
parseISO
function that parses ISO 8601 strings. See #1023.
Add constants that can be imported directly from date-fns
or the submodule date-fns/constants
:
maxTime
minTime
New locales:
Added new function differenceInBusinessDays
which calculates the difference in business days. Kudos to @ThorrStevens!
Added new function addBusinessDays
,
similar to addDays
but ignoring weekends. Thanks to @ThorrStevens!