Skip to content Skip to sidebar Skip to footer

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 i

Solution 1:

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 = newDate(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 e.g. Klingon).2) Different behavior on different browsers3) 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

Solution 2:

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 = newDate(2222, 11, 18);
let strDate = customDate.toLocaleDateString();
let format = strDate
    .replace("12", "MM")
    .replace("18", "DD")
    .replace("2222", "yyyy");

Solution 3:

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/

Solution 4:

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 = newDate(2019,0,31);
    console.log(my_date.toLocaleDateString());
    // Initialize variablesvar 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";
    } elseif (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";
    } elseif (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";
    } elseif (date_parts[2] == 31) {
        third = "dd";
    } else {
        if (date_parts[2].length <= 2) {
            third = "mm";
        }
        else {
            third = "mmm";
        }
    }

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

Solution 5:

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"; // e.g. 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;
    }

Post a Comment for "Javascript - Get System Short Date Format"