简体   繁体   中英

JavaScript - Get system short date format

Is there any way to get system short date format in JavaScript?

For example whether system's short date is in American format eg. m/d/Y or in european eg. d/m/Y

Please note: This is not question about formatting date or calculating it based on geolocation, but about getting the format from the OS/system

After a pinch of research I concluded that technically it's not possible to get regional settings -and by this, date format- but you can do several other things. Pick one of these options:
a) The already mentioned -and outdated- "toLocaleString()" function:

 var myDate = new Date(1950, 01, 21, 22, 23, 24, 225); var myDateFormat = myDate.toLocaleString(); alert (myDateFormat);

ISSUES:
1) You can't "myDateFormat.replace" to get the date mask as month is not stored as "01", "02", etc in the string but as text instead, based on locale (like "February" in English but it's "Φεβρουάριος" in Greek and who knows what in eg Klingon).
2) Different behavior on different browsers
3) Different behavior on different OS and browser versions...

b) Use the toISOString() function instead of toLocaleString() . You won't get the locale date mask but get a date from which you can tell where's which part of the date (ie where "month" or "day" is in that string). You can also work with getUTCDate() , getUTCMonth() and getUTCDay() functions. You still can't tell what date format the client uses, but can tell which Year/Month/Day/etc you work with when you grab a date; use the code above to test the functions I mentioned here to see what you can expect.

c) Read Inconsistent behavior of toLocaleString() in different browser article and use the (IMHO great) solution described there

for my case i used a custom date that i know what number is day, what is month and what is year so it can possible with a simple replace statement.

let customDate = new Date(2222, 11, 18);
let strDate = customDate.toLocaleDateString();
let format = strDate
    .replace("12", "mm")
    .replace("18", "dd")
    .replace("2222", "yyyy");

It is not possible. You can get culture from user browser and use some js libraries to convert to correct date format. http://code.google.com/p/datejs/

I made a function to determine the client date format. The function determine the date format separator, and also determine the 1st, 2nd and third part of the date format.

getDateFormat(){
    // initialize date value "31st January 2019"
    var my_date = new Date(2019,0,31);
    console.log(my_date.toLocaleDateString());
    // Initialize variables
    var separator="";
    var first="";
    var second="";
    var third="";
    var date_parts = [];

    // get separator : "-", "/" or " ", format based on toLocaleDateString function        
    if (my_date.toLocaleDateString().split("-").length==3){
        separator = " - ";
        date_parts = my_date.toLocaleDateString().split("-");
    } 
    if (my_date.toLocaleDateString().split("/").length == 3) {
        separator = " / ";
        date_parts = my_date.toLocaleDateString().split("/");
    } 
    if (my_date.toLocaleDateString().split(" ").length == 3) {
        separator = " ";
        date_parts = my_date.toLocaleDateString().split(" ");
    } 

    // get first part        
    if (date_parts[0]==2019){
        first ="yyyy";
    } else if (date_parts[0] == 31){
        first = "dd";
    } else{
        if (date_parts[0].length<=2){
            first ="mm";
        }
        else{
            first="mmm";
        }
    }

    // get second part        
    if (date_parts[1] == 2019) {
        second = "yyyy";
    } else if (date_parts[1] == 31) {
        second = "dd";
    } else {
        if (date_parts[1].length <= 2) {
            second = "mm";
        }
        else {
            second = "mmm";
        }
    }

    // get third part        
    if (date_parts[2] == 2019) {
        third = "yyyy";
    } else if (date_parts[2] == 31) {
        third = "dd";
    } else {
        if (date_parts[2].length <= 2) {
            third = "mm";
        }
        else {
            third = "mmm";
        }
    }

    // assembly
    var format = first + separator + second + separator + third;
    console.log(format);
    return format;
}

I've created a workaround to determine which format the user's browser is using. This is in C# but the logic is the same:

Here are the steps:

  1. First try to convert the user's browser date into American format (mm-dd-yyyy). Convert.ToDateTime is using the American date format.

  2. If that fails it means the user is using European format (dd-mm-yyyy). However, this will only cover the day 13 to 31 because this is not a valid month.

  3. If the conversion is successful, do another check to determine if the converted date is between the current UTC day + 1 day (to cover UTC+14) and current UTC day - 1 day (to cover UTC-12). https://www.timeanddate.com/time/current-number-time-zones.html

  4. If the converted date is out of the current date range, it means the user's browser is using European format (dd-mm-yyyy) and you can convert it to American format if you want.

     string localeDateString = "01/11/2020"; // eg input is using European format (dd-mm-yyyy) var localeDate = new DateTime(); try { localeDate = Convert.ToDateTime(localeDateString); //var checkTheFormatOfDateInput = localeDate.ToLongDateString(); var currentDateTime = DateTime.UtcNow; //var currentDateTime = Convert.ToDateTime("11/01/2020"); //var checkTheFormatOfCurrentDate = Convert.ToDateTime("11/01/2020").ToLongDateString(); var currentDateTimePositive = currentDateTime.AddDays(1); var currentDateTimeNegative = currentDateTime.AddDays(-1); var outOfCurrentDateRange = !(localeDate.Ticks > currentDateTimeNegative.Ticks && localeDate.Ticks < currentDateTimePositive.Ticks); if (outOfCurrentDateRange) { localeDate = DateTime.ParseExact(localeDateString, "dd/MM/yyyy", CultureInfo.InvariantCulture); } } catch { localeDate = DateTime.ParseExact(localeDateString, "dd/MM/yyyy", CultureInfo.InvariantCulture); } //var checkTheEndResultFormat = localeDate.ToLongDateString();

Below is the clean code wrapped in a method:

    private DateTime ConvertAmericanOrEuropeanDateFormatToAmericanDateFormat(string localeDateString)
    {
        var localeDate = new DateTime();

        try
        {
            localeDate = Convert.ToDateTime(localeDateString);
            var currentDateTime = DateTime.UtcNow;
            var currentDateTimePositive = currentDateTime.AddDays(1);
            var currentDateTimeNegative = currentDateTime.AddDays(-1);

            var outOfCurrentDateRange = !(localeDate.Ticks > currentDateTimeNegative.Ticks && localeDate.Ticks < currentDateTimePositive.Ticks);

            if (outOfCurrentDateRange)
            {
                localeDate = DateTime.ParseExact(localeDateString, "dd/MM/yyyy", CultureInfo.InvariantCulture);
            }
        }
        catch
        {
            localeDate = DateTime.ParseExact(localeDateString, "dd/MM/yyyy", CultureInfo.InvariantCulture);
        }

        return localeDate;
    }

A very good but lengthy answer can here found here: https://stackoverflow.com/a/9893752/2484903

A shorter one here:

 let customDate = new Date(2222, 3, 8); let strDate = customDate.toLocaleDateString(); let format = strDate .replace("04", "MM") .replace("4", "M") .replace("08", "dd") .replace("8", "d") .replace("2222", "yyyy") .replace("22", "yy"); console.log(format);

We create a date object of a known date and then parse the outcome.

First we look for "04" (which corresponds to 3 from the date definition); that would be the two digit month format MM . If not found, it must be the single digit format M . Afterwards do the same for day and year.

It should do the job...

function getSystemDateLocale(){
    let testDate = (new Date('2000-1-30')).toLocaleDateString()
    if (testDate.substring(0,2) == '30') return 'EU'
    else return 'US'
}

使用 Date.CultureInfo.formatPatterns.shortDate

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