A tool for upgrading date-fns versions
npm install @date-fns/upgradeSupport package with helper functions for date-fns upgrade from 1.x to 2.x
Codemod that will help you apply these helpers automatically located here -
date-fns-upgrade-codemod.
``bash`
npm install @date-fns/upgrade --saveor with yarn
yarn add @date-fns/upgrade
convertTokens is a helper function used for 2nd argument of format functionYYYY
to convert date tokens like to new format.
See this post
for more details.
`diff
+import { convertTokens } from '@date-fns/upgrade/v2'
const formattedDate = format(
new Date(),
- 'YYYY',
+ convertTokens('YYYY'),
)
`
date-fns@2.x functions don't accept string as arguments any more (see CHANGELOG), legacyParse is used to simplify that transition, it uses algorithm from 1.x to do that. See this post for details on "why" this was done.
`diff
+import { legacyParse } from '@date-fns/upgrade/v2'
const formattedDate = format(
- '2014',
+ legacyParse('2014'),
'YYYY',
)
`
legacyParseMap is used same as legacyParse but for arguments that accept arrays.
`diff
+import { legacyParseMap } from '@date-fns/upgrade/v2'
var dateToCompare = new Date(2015, 8, 6)
var datesArray = [
'2014-01-01',
'2015-01-01'
]
-var result = closestIndexTo(dateToCompare, datesArray)
+var result = closestIndexTo(dateToCompare, legacyParseMap(datesArray))
``