Search code examples
javascriptjqueryjquery-globalizationjquery-globalize

How to get a weekday in Globalize?


In my project, I am using Globalize 1.1.1.

It has the globalize for weekday in this path "dates/calendars/gregorian/days", I don't know how to get a specific day.

Like I want to get the Globalize for "Thuesday", the field is called "thu".

So my question is, How do I do it?

I had tried:

  • Globalize.dateParser({ raw: "weekday/wide" })( "thu" )
  • Globalize.formatUnit(1, "days", { form: "wide" })
  • Globalize.formatMessage("/dates/calendars/gregorian/days/wide/thu")

Solution

  • Use CLDR data directly

    var dayNames = Globalize.cldr.main('dates/calendars/gregorian/days/format/wide');
    
    // Access Thursday
    dayNames.thu;
    
    // Unrelated to question, but first day of week might be useful as well
    var firstDay = dayNames[Globalize.cldr.supplemental.weekData.firstDay()];
    

    Use date formatters

    ... or rely on date calculations, which are less dependent on cldr as noted by Rafael Xavier in this globalizejs issue on github.

    So you would create a date, which you know is Tuesday, and specify a format giving you the weekday:

    var formatter = Globalize.dateFormatter({raw: "EEEE"});
    formatter(new Date(0, 0, 2));