Replies: 1 comment
-
Luxon already has a As for accepting strings: This is not something we can really do. You want ISO, but the next person might want a Unix timestamp (seconds or milliseconds or maybe even nanos?) or RFC 2822, etc. etc. This is something that needs to be solved in application logic. You know best which formats you have, you need to tell Luxon that. And I don't think Additionally, I don't understand how Luxon having a |
Beta Was this translation helpful? Give feedback.
-
May I get some advice on how to parse dates that might come in one of several formats?
I tried searching but having trouble to find any advice or documentation about this case. Please let me know if I missed something or if it has already been asked.
There are two cases I have run across where I feel this is a little awkward in Luxon:
Case 1: Hybrid Node.js and Browser app (for example, using Next.js)
I run into an issue where a function could be getting Date or ISO string. For example, I have a User object that comes from the Node.js database adapter like this:
Sometimes this user object is served to the frontend as a serialized JSON blob.
As is well known, Date is serialized into an ISO string:
console.log(JSON.stringify(new Date())); // "2022-03-01T02:53:42.510Z"
I want to write a function that checks if the user account is expired, but I also want it to be portable. In other words, the function does not know if it will run in the browser or server so I write something like:
I feel like the my parsing strategy is little bit awkward, though overall I am quite happy writing code like this for myself, I like starting with the strictest API and being explicit about what formats can be accepted. And accepting Date and ISO string seems like a pretty good compromise too, with a low chance of parsing errors.
However, I noticed some people, when facing this kind of strict API, start short circuiting the parsing logic by wrapping it in nested
new Date()
etc so it becomes very very unstrict:Actually, I feel like this case is especially difficult for a few reasons:
new moment(...)
where the source format was extremely flexible. So of course, whenmoment()
is gone, they naturally want to reach for the next best thing,new Date(...)
.new Date()
with unknown input is especially "dangerous" because the system won't give any warnings if you use a weird format. If the code used some wrapper function that parsed the dates, it could generate warnings or more useful errors if a suspicious input is received.Case 2: React Wrapper
I tried to create a React wrapper to show a formatted date. Depending on the source (server, recalculation on every form input, etc), I might be starting with a Date, DateTime or ISO string. As this is the final formatting step, not an internal calculation, I think seems like the most useful API is to just accept any of these and will have a low chance of showing incorrect data:
The awkward part here is I had to write my own quite long
parseDate()
function. And, I'm not quite sure if it is correct. For example, I'm not really sure if an invalid input will throw an error or generate aninvalid
DateTime (without reading the documentation carefully...), or if the error message generated by an invalid input will be enough information to debug it.In both of these cases, I feel like there is a missing piece to parse from one of multiple formats. Should I be writing wrappers like
parseDate()
above in all of my projects? Is my strategy too loose thus I am not quite "getting it"?While I'm not trying to make a feature request specifically, if Luxon had have some "parse multiple formats" function which looked like this:
and that:
I think i would have just used that function instead of wondering how to handle this case. Some kind of official documentation or code snippet could also serve to guide developers in how to handle this in an ideal way. Thanks!
Beta Was this translation helpful? Give feedback.
All reactions