Consider bringing back old parse #2231
-
Hi all! Today I tried the latest I'm sure there are plenty of reasoning behind the change, but for all the pleasant updates we've found (
Default module: const { parse } = require('date-fns');
parse(new Date()); // <- Throws `TypeError: 3 arguments required, but only 1 present` FP module: const { parse } = require('date-fns/fp');
parse(new Date()); // <- Returns a curried function With all that being said, we'd really like to continue using, testing and supporting next |
Beta Was this translation helpful? Give feedback.
Replies: 15 comments 6 replies
-
@nfantone |
Beta Was this translation helpful? Give feedback.
-
@leshakoss Hi and many thanks for your fast response! Very glad to read that you kept the functionality. May I ask the reason behind the name swapping? Wouldn't it have been easier (as far as API understanding and easiness of migration go) to keep |
Beta Was this translation helpful? Give feedback.
-
@leshakoss Another conspicuous detail that we found are the new pattern tokens. Docs for // v1
format(new Date('2018-01-01'));
// > '2018-01-01T00:00:00.000+00:00'
// v2
format('YYYY-MM-DDTHH:mm:ss.SSSZ')(new Date('2018-01-01'))
// > '2018-01-01151476480000000:00:00.000Z' What should the corresponding pattern be now? |
Beta Was this translation helpful? Give feedback.
-
@nfantone I am inclined to reply "for historical reasons" because it was decided two years ago, but it would be unfair to say that about a not yet released version. But it certainly feels like ancient history for me by now :-) Basically, In a lot of aspects, v2 is what For your second question, the pattern would be |
Beta Was this translation helpful? Give feedback.
-
@nfantone I hope @leshakoss did address your questions. If you still have some more — don't hesitate to ask. Also, thank you for trying v2, it's much appreciated. |
Beta Was this translation helpful? Give feedback.
-
@leshakoss - It looks like |
Beta Was this translation helpful? Give feedback.
-
@green-arrow you can just use parse |
Beta Was this translation helpful? Give feedback.
-
@dkozickis - The It doesn't appear that there are any public methods in function parseArbitraryString(value: string): Date |
Beta Was this translation helpful? Give feedback.
-
@green-arrow the old parse essentially equals |
Beta Was this translation helpful? Give feedback.
-
that is not crossbrowser solution as new Date or Date.parse work differently per browser. so latest versions of date-fns in my opinion missing important parse functionality. does anyone know a working workarounds (except using version < v2)? |
Beta Was this translation helpful? Give feedback.
-
I feel like you are talking about two different things in this thread 🙂
|
Beta Was this translation helpful? Give feedback.
-
@leshakoss |
Beta Was this translation helpful? Give feedback.
-
Except it's not. I've read this several times in this thread now and I believe it's misleading people. Old const parse = require("date-fns/parse")
parse('2020/05/05'); // Tue May 05 2020 00:00:00 GMT+0100
const parse = require("date-fns/parseISO")
parse('2020/05/05'); // Invalid Date It was also stated before that "old
Well - it was implemented before. Despite its shortcomings, it was old
Which sounds exactly what people seem to be requesting. |
Beta Was this translation helpful? Give feedback.
-
@nfantone the things are much more straightforward than discussed in the thread. Sorry for the confusion. Simply use new Date('2020/05/05')
//=> Tue May 05 2020 00:00:00 GMT+0800 (Singapore Standard Time)
Date.parse('2020/05/05')
//=> 1588608000000 As @Qvatra noted, this is not consistent across browsers (but will work for you example) and v1 didn't fix that, and this is why we removed it in the first place. It's impossible to write a universal string parser and this inconsistency is the evidence of that. If you have to parse something like |
Beta Was this translation helpful? Give feedback.
-
I'd like to share my approach to replacing (and even go beyond) the functionality that previous /**
* Tries to parse the given input as a `Date`.
* - If the input is a numerical string (`[0-9]` digits only), it'll be coerced into a number
* and treated as milliseconds since the epoch.
* - If the input is a non-numerical `String`, `date-fns~parseJson` will be used to parse it as an ISO 8601 date.
* - If the input is a non-numerical `String` and `date-fns~parseJson` fails, a series of supported format
* strings will be attempted until one results in a valid `Date` (or non succeed).
* - If the input is a `Number`, it'll be passed as-is to the `Date` constructor.
* - If the input is already a `Date`, it'll be returned without modifications.
* - Otherwise, `Invalid date` is returned.
*
* @function
* @param {string|number|Date} dt The value to parse.
* @returns {Date} The parsed date.
*/
const tryParseJson = cond([
[isNumber, ms => new Date(ms)],
[
isString,
ifElse(
// Is it a numerical string?
test(/^\s*\d+\s*$/),
compose(ms => new Date(ms), Number, trim),
// Replace any `/` with `-` to improve `date-fns~parseJson` chances of
// successfully parsing the date
compose(dt => tryParseDate(dt), replace(/\s*\/+\s*/g, '-'))
)
],
[instanceOf(Date), identity],
[T, () => new Date(NaN)]
]); The private /**
* Supported `date-fns/parse` format strings. Patterns will be attempted sequentially
* until one of them returns a valid `Date` or all were exhausted. ISO 8610 format is always
* included (i.e.: there's no need to explicitly add it below as a supported format string).
*
* @type {string[]}
*/
const patterns = Object.freeze(['MM-dd-yyyy']); |
Beta Was this translation helpful? Give feedback.
@nfantone the things are much more straightforward than discussed in the thread. Sorry for the confusion.
Simply use
new Date()
orDate.parse()
to parse free-form strings. This is how oldparse
worked and unless you know why you needparse
/parseISO
/parseJSON
/etc, you should not use any of that.As @Qvatra noted, this is not consistent across browsers (but will work for you example) and v1 didn't fix that, and this is why we removed it in the first place. It's impossible to write a universal string parser and this inconsistency is the evidence of that.
If you…