Click here to Skip to main content
14,240,044 members
Rate this:
Please Sign up or sign in to vote.
Hello

I need client machine date and time format which is set by user in javascript.
Not at server side.

Thanks Regards
Rakesh Jogani
Posted
Updated 12-Jul-19 8:19am
Comments
   
What have you tried and where is the problem?
ZurdoDev 29-Jan-14 9:11am
   
Where are you stuck? In JS you can do var t_date = new Date();
Rakesh Jogani 29-Jan-14 9:18am
   
i get only which type of date format is set in client machine.
ZurdoDev 29-Jan-14 9:23am
   
I don't understand what you need then.
Rate this:
Please Sign up or sign in to vote.

Solution 1

You can not in a way of calling a function - JavaScript does not reveal information about the underlying OS, for security reasons.
You may use toLocaleDateString() of Date object to get a string of a specific date (like 12/13/2014) and then analyze the string to decide what was the original formatting string...
   
Comments
SanjeevkumR 18-Jun-14 2:49am
   
If we get the client machine's date as like "06-10-2014" in our Javascript, then how to decide which is the month/date? may be either "06" is month or "10" is month. In this case, the system gets confused and may display wrong result.
Is any other way to solve this?
Even am facing the same situation.
Rate this:
Please Sign up or sign in to vote.

Solution 2

Hi, 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;
    }
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100