globalizejs / globalize

A JavaScript library for internationalization and localization that leverages the official Unicode CLDR JSON data
https://globalizejs.com
MIT License
4.8k stars 603 forks source link

DateParser returns null on 'yMdhms' with "en-US" locale #820

Closed szahn closed 6 years ago

szahn commented 6 years ago
var Globalize = require( "globalize" );

Globalize.load(
    require("cldr-data/supplemental/likelySubtags.json"),
    require("cldr-data/main/en/numbers.json"),
    require("cldr-data/supplemental/numberingSystems.json"),
    require("cldr-data/main/en/ca-gregorian.json"),
    require("cldr-data/main/en/timeZoneNames.json"),
    require("cldr-data/supplemental/timeData.json"),
    require("cldr-data/supplemental/weekData.json")
);

const dateTime = "4/8/2019 4:25:30 PM";
const parseFormat = "yMdhms";
const globalize = Globalize("en");
const dateParser = globalize.dateParser({skeleton: parseFormat});
const parsed = dateParser(dateTime) || globalize.parseDate(dateTime);
//why parsed is null?

A desired behavior would be to throw an exception or return an error result describing why the date time could not be parsed given the skeleton.

rxaviers commented 6 years ago

Globalize throws exceptions on developer errors such as API misuse (e.g., invalid argument type). For values that can't be parsed on runtime, the principle is similar to JavaScript functions such as parseInt() that returns the parsed value or NaN.

Having said that, we could return additional information, feel free to suggest how this could be made.

Parser will always work for formatted values, so one way to see how input should look like is to format it, e.g.:

Globalize('en').dateFormatter({skeleton: "yMdhms"})(new Date());
// > '4/12/2018, 10:47:53 AM'

It's not a goal of the parser to parse any kind of input, but to be an inverse operation for the formatter. It's therefore better to use parser on an assisted UI such as https://github.com/rxaviers/react-date-input (that uses format to parts to generate the UI).

rxaviers commented 6 years ago

I'm closing this since it's not an issue. I can re-open if we figure out any action item that could be done from this thread. Thanks!

szahn commented 6 years ago

This isn't very helpful. I still don't know why that value can't be parsed. From your example above, it seems like the only reason the parsing failed was because a comma was missing between the date and time. Why is the parsing so brittle? More examples of correctly parsing date and time using skeletons would be good, at least explain that a delimiter other than a space is required between date and time to be parsed together as one. I'd rather use MomentJS which automatically parses date time strings.

rxaviers commented 6 years ago

This isn't very helpful. I still don't why that value can't be parsed

Missing comma

More examples of correctly parsing date and time using skeletons would be good

You could run node and format any skeleton to check the output https://github.com/globalizejs/globalize/#getting-started

I'd rather use MomentJS which automatically parses date time strings.

It's a great library, except for i18n, it doesn't follow CLDR.