-
Notifications
You must be signed in to change notification settings - Fork 152
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
Support custom format strings when parsing date strings #796
Comments
Thanks for the feedback! So far, we have mostly considered parsing (other than deserialization of ISO 8601-like strings) to be in the domain of business logic and outside the scope of Temporal (since everybody knows their own use case for parsing other formats better than Temporal possibly can, and to avoid repeating the mistakes of That said, if we did add it I would recommend a separate method than |
I think that this : Temporal.Date.fromFormat(dateString: string, format: string); Would be a good solution. |
Thank you for taking this into consideration! I totally agree with you @ptomato, I also agree that the mistakes of You could have a strict parser that throws if the format string doesn't contain the necessary tokens required to create an instance of a given class. // Throws because the format string doesn't contain any token to define
// the 'day' part necessary to construct a Temporal.Date
const date = Temporal.Date.fromFormat('2020-08-01', 'YYYY-MM') The same goes for when the date string matches the format string, but the parsed values are invalid. // Throws because 45 is an invalid month
const date = Temporal.Date.fromFormat('2020-45-01', 'YYYY-MM-DD') Possibly, you would want to support an optional third options argument to configure // Creates a Temporal.Date instance with 2020-12-01
const date = Temporal.Date.fromFormat('2020-45-01', 'YYYY-MM-DD', { disambiguation: 'constrain' }) As a side-note, in moment.js the parser is loose by default. According to the docs:
You would manually have to set it to In my opinion, that is a mistake though. In I also agree with you that it would probably be a better idea to implement this as a separate |
Note to myself that this would also need first-class support for non-Gregorian calendars - e.g. |
FYI java's DateTimeFormatter https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html The constructor-from-string fns on the individual entities, Date, Time etc use this under the hood. DateTimeFormatter can be customised with locales, calendars etc. |
I also am looking for a way to get an arbitrarily-formatted string based on common patterns, like |
re: formatting, here's an excerpt from date-fns 2.x documentation (https://date-fns.org/v2.16.1/docs/format) :
If we do use a microformat formatter, following the standard above seems like a reasonable starting point |
More than just a reasonable starting point, this is the closest thing I've seen to an authoritative canonical spec. Cool find. |
I too would vote for parsing and formatting with a format string. I think it will have two advantages.
I am writing this on a day when we had major outage because of date formatting issue. To create a local date format of yyyy+(m+1)-dd. The month being 0 indexed we had a small bug of matching with '<10' for padding and it was interpreted as 9, and got padded '2020-010-01', bam! timebomb went off and multiple services in multiple environments started going down all at the same time. Only if toLocalJSON() or something like that would have been present I am sure developer would have used a substring to get the formatted date portion. |
Just to say, parsing/formatting needs to be locale-aware - and locale-aware bundles for Moment, js-joda etc are big, ie about 45KB for a bundle just containing en-US alone (e.g. https://www.npmjs.com/package/@js-joda/locale). So not having to send down these huge payloads to client browsers if a parser+formatter gets included in Temporal would be a huge win IMO |
This is not included in the Temporal proposal that's currently under review, but we can move the discussion to Temporal V2: js-temporal/proposal-temporal-v2#2 |
Apologies if this has already been up for discussion, but I couldn't find any issue on it, and nothing in the documentation that indicate that this is already supported.
When working with legacy or third-party systems, you often find yourself receiving date strings in the most unwieldy formats. To remedy this, date libraries often support custom format strings to make it possible to parse such strings.
Moment.js support passing a format string as a second argument, as defined in the docs.
Something like:
moment("07-28-2020", "MM-DD-YYYY")
Luxon also support this through its
.fromFormat()
method.Like this:
DateTime.fromFormat("07-28-2020", "MM-DD-YYYY")
This is a feature I've used frequently over the years, and one of the reasons I've used third-party libraries instead of the
Date
object.To add support for this in
Temporal
, one approach could be to extend the options object of the.from()
method to also take an optional format string.Another approach could be to add an additional static method that takes the format as a second argument (like Luxon):
I believe this would be convenient for at least
Temporal.Absolute
andTemporal.DateTime
, but possibly also for all the other human-readable classes likeTemporal.Date
,Temporal.Time
etc.The text was updated successfully, but these errors were encountered: