New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
parseJSON - minimal implementation for parsing JSON dates from an API #1463
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I love it! However, I think the name is confusing. I like parseJSON
more because it's date string formatted by date.toJSON
, and I believe that the fact that only UTC dates are supported is an implementation detail.
Thanks for the feedback. I agree calling it |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work, thank you a lot! Besides the small issue with the function name, the PR is ready to go.
Please add |
Thank you for reviewing and accepting this @kossnocorp! The remaining issues have been fixed. |
Thank you! I'll ship it with the next release. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
+parts[4], | ||
+parts[5], | ||
+parts[6], | ||
+(parts[7] || 0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yay, we have this in a few places in our app
I've shipped the PR with |
…d with up to 6 digits in the milliseconds field (date-fns#1463)
Is there an argument against simply using the parsing capabilities built into JS's new Date('2000-03-15T05:20:10.123Z') Is it a matter of trust in the format supplied by the (commonly) API that would necessitate using a library-supplied function such as this? |
@alexdilley yes it is simply about trust in the underlying implementation of different browsers. These days that might not be as relevant as it once was, but using this function leaves no room for doubt. |
The ISO date parsing previously done in
toDate
was moved intoparseISO
to keep the minimal build size low when string date manipulation is not required (#1016). The problem is that any application that retrieves a date from an API will parse some form of serialized date string before passing it to other functions. The following is typical example:As soon as
parseISO
is used in this way the benefit of removingtoDate
has been negated. TheparseISO
andparse
functions are still around 7000 and 8000 bytes respectively. The proposedparseUTC
function is only ~520 bytes in size and sufficient to parse the standard JSON serialized date string formats of most server-side languages.Aside from complete ISO dates and the input types accepted by
toDate
the function returns anInvalid Date
. The intention of this function will not be to parse user input, for thisparse
would still be needed, but it permits convenient parsing of date strings from an API with a known ISO format.This is a proposed compromise/solution for the issue I raised here, and something I have used in a couple of projects recently to good effect.
I am still undecided about the best name for this function. My initial thought was
parseJSONDate
, but then I realized only UTC dates are supported and calling itparseUTC
might be more generic and describe the function more specifically.I would like to propose this function for inclusion in the core
date-fns
library, however, if it is deemed not a good fit I can also add it todate-fns-tz
.