Domanda

Assuming that the European(UK) locale date format uses days before the month and year like this:

EEE, dd MMM yyyy

When I use that in toLocaleTimeString with option weekday= short, I get inconsistent response. Days come after the month.

Fri, Sep 27, 2013 18:38:30 United Kingdom Time

However, when I use the weekday=long it will be consistent with expected date format.

Friday, 27 Sep 2013 18:38:30 United Kingdom Time

Whether using a short format or long I assume it should be consistent and have the days before the months. However, this is not the case, I am not sure if this is a desired behavior or I am missing some point?

Here is the JavaScript Code in Chrome V29:

    var options = {
        hour: "2-digit",
        minute: "2-digit",
        second: "2-digit",
        year: "numeric",
        month: "short",
        day: "2-digit",
        hour12: false,
        timeZoneName: "short",

        weekday:"short"  //"long"
    }

    var date = new Date("Fri Sep 27 2013 13:38:30 GMT-0400");
    options.timeZone = 'America/New_York'
    console.log(date.toLocaleDateString("en-us",options))

    options.timeZone = 'Europe/London'
    console.log(date.toLocaleDateString("en-gb",options))

Output:

with weekday= short

Fri, Sep 27, 2013 1:38:30 PM ET
Fri, Sep 27, 2013 18:38:30 United Kingdom Time

with weekday= long

Friday, Sep 27, 2013 1:38:30 PM ET
Friday, 27 Sep 2013 18:38:30 United Kingdom Time

Expected:

Fri, Sep 27, 2013 1:38:30 PM ET
Fri, 27 Sep 2013 18:38:30 United Kingdom Time

È stato utile?

Soluzione

One can omit the year in options and get the day-month format correctly but a date string is obviously no good without a year value:

var options = {
    hour: "2-digit",
    minute: "2-digit",
    second: "2-digit",
    //year: "numeric",
    month: "short",
    day: "2-digit",
    hour12: false,
    timeZoneName: "short",

    weekday:"short"
}

Returns:

Fri, Sep 27 1:38:30 PM ET
Fri 27 Sep 18:38:30 United Kingdom Time 

So here is a temporary workaround until we find out what is going on with this; You can set the weekday separately for en-GB to get the desired outcome, but you end up with long weekday names:

var options = {
        hour: "2-digit",
        minute: "2-digit",
        second: "2-digit",
        year: "numeric",
        month: "short",
        day: "2-digit",
        timeZoneName: "short",
        weekday: "short"
}

    var date = new Date("Fri Sep 27 2013 13:38:30 GMT-0400");
    options.timeZone = "America/New_York";
    console.log(date.toLocaleDateString("en-US",options));

    options.timeZone = "Europe/London";
    options.weekday = "long";
    console.log(date.toLocaleDateString("en-GB",options));

This returns:

Fri, Sep 27, 2013 1:38:30 PM ET
Friday, 27 Sep 2013 18:38:30 United Kingdom Time 

I'm still trying to find the cause of this. This doesn't seem to be an ICU issue which returns expected values for both locales (see the result for en-GB and en-US). It might be that asking for a format with short weekday names and year causes the format matcher to fall back to en (which uses E, MMM d y). Finding out whether this is a defect or not, needs more looking into. I will update here if I find out.

One small note about explicitly setting the hour12. Considering that the default value of this is locale-dependent, I believe it's better to leave that out, too (it's overridden anyway as you see in the output time format for en-GB).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top