简体   繁体   中英

How to use Globalize 1.x to parse date

Can someone show me how to use Globalize 1.x to parse date according to a given culture? Without using bower or node.

I've seen the example on the official site, but there they statically load the I18n unicode CLDR using static JSON. I need to load dynamically accordingly to the user settings. I've seen also the "How do I load CLDR data into Globalize" on the official page. There they show how to load CLDR in a dynamic way.

My problem is that I can not figure out which CLDR json file I need to simply parse string to date and format date to string accordingly to a given culture like de-CH (for example).

I can not find anywhere a concrete and complete example of such scenario.

I took the liberty to break your question down into four questions below:

  1. How to parse a date?

More information at https://github.com/jquery/globalize/blob/master/doc/api/date/date-parser.md

// After having fed Globalize on CLDR data, do:
var parser = Globalize.dateParser(pattern);
parser("24.7.2015");
// > "Fri Jul 24 2015 00:00:00" if Globalize.locale("de-CH").
  1. What are the available locales?

Unicode CLDR is available as JSON at https://github.com/unicode-cldr/ and currently offered in two different sizes: the "modern" coverage (which contain the set of locales listed as modern coverage targets by the CLDR subcomittee) and the "full" coverage (which contain the complete set of locales, including those in the corresponding modern packages).

The available locales of each packages are available here https://github.com/unicode-cldr/cldr-core/blob/master/availableLocales.json . Alternatively, it can also be found by looking at this directory: https://github.com/unicode-cldr/cldr-dates-full/tree/master/main for the "full" coverage (analogous for the "modern" one). Technical details of the lookup matcher can be found https://github.com/rxaviers/cldrjs/blob/master/doc/bundle_lookup_matcher.md .

  1. How to load CLDR data dynamically?

Unless you implement your own ajax fetcher, I suggest that you use either AMD or jQuery. There are basic examples for each at https://github.com/jquery/globalize/blob/master/doc/cldr.md#how-do-i-load-cldr-data-into-globalize .

  1. Which CLDR files I need to load for date parsing?

On the table of https://github.com/jquery/globalize#2-cldr-content , see the files in "Date module" + "Number module" + "Core module".

Just let me know on any further questions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM