16

Is there a possibility to determine, with pure Javascript, what date time FORMAT has user configured on his operating system (Windows, Linux, MAC OS, etc.)?

Thanks in advance.

EDIT: I know about the method toLocaleString(), but this isn't help me to get the format that client has configured on his local machine.

Sergiu
  • 1,397
  • 1
  • 18
  • 33
  • 2
    Are you asking how to get the user's datetime format, or for the timezone they are in? Most of the question sounds like the former, but then your ending, "UTC" or "Local time (based on my system settings)", sounds like you want the zone. – Rob Levine Apr 05 '11 at 08:03
  • 1
    No, I don't want the zone. I want the format. Thanks for reply, I edited the question. – Sergiu Apr 05 '11 at 08:06

3 Answers3

3

I wrote something in pure javascript that works in IE/Firefox/Chrome. It will out put MM/DD/YYYY or DD/MM/YYYY,... depending in toLocalDateString().

Did not work on Safari but new Date().toLocalDateString() did not either.

Here is a jsFiddle

 //Create a known date string
var y = new Date(2013, 9, 25);
var lds = y.toLocaleDateString();

//search for the position of the year, day, and month
var yPosi = lds.search("2013");
var dPosi = lds.search("25");
var mPosi = lds.search("10");

//Sometimes the month is displayed by the month name so guess where it is
if(mPosi == -1)
{
    mPosi = lds.search("9");
    if(mPosi == -1)
    {
        //if the year and day are not first then maybe month is first
        if(yPosi != 0 && dPosi != 0)
        {
            mPosi = 0;
        }
        //if year and day are not last then maybe month is last
        else if((yPosi+4 <  lds.length) && (dPosi+2 < lds.length)){
            mPosi = Infinity;
        }
        //otherwist is in the middle
        else  if(yPosi < dPosi){
            mPosi = ((dPosi - yPosi)/2) + yPosi;            
        }else if(dPosi < yPosi){
            mPosi = ((yPosi - dPosi)/2) + dPosi;
        }   
    }

}


var formatString="";
var order = [yPosi, dPosi, mPosi];
order.sort(function(a,b){return a-b});

for(i=0; i < order.length; i++)
{
    if(order[i] == yPosi)
    {
        formatString += "YYYY/";
    }else if(order[i] == dPosi){
        formatString += "DD/";
    }else if(order[i] == mPosi){
        formatString += "MM/";
    }
}

formatString = formatString.substring(0, formatString.length-1);

$('#timeformat').html(formatString+" "+lds);
Nick
  • 3,217
  • 5
  • 30
  • 42
  • I liked this suggestion by Nick, but I also needed a way to try to detect the date separator (in my case a period instead of the forward slash) so I added a dirty check for it: http://jsfiddle.net/cK82m/1 – Eirik H Nov 13 '13 at 10:19
1

Here's an idea, that may or may not work.

Create a date where all the elements are distinct, like February 18th 1999 at 13:45, use toLocaleString(), then identify the elements based on their distinct values.

Could be kind of complicated and I don't have any code that might help with it, but it's an idea to be thrown out there, maybe you can make use of it.


EDIT: Here's some code:

var d = new Date(1999,1,18,13,45,0).toLocaleString();
document.write("<p>String: "+d+"</p>");
var f = d
    .replace(/1999/,"%Y")
    .replace(/99/,"%y")
    .replace(/F[^ ]{3,}/i,"%M")
    .replace(/F[^ ]+/i,"%m")
    .replace(/PM/,"%A")
    .replace(/pm/,"%a")
    .replace(/18[^ ]+/,"%d%S") // day number with suffix
    .replace(/18/,"%d")
    .replace(/13/,"%H")
    .replace(/1/,"%h")
    .replace(/45/,"%i")
    .replace(/00/,"%s");
    // optionally add something to detect the day of the week (Thursday, here)
document.write("<p>Format: "+f+"</p>");

Output:

String: 18 February 1999 13:45:00
Format: %d %M %Y %H:%i:%s
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
0

Something like this ?

<script type="text/javascript">

    var d=new Date();
    document.write("Original form: ");
    document.write(d + "<br />");
    document.write("Formatted form: ");
    document.write(d.toLocaleString());

    //calculate change of the 2 dates
</script>
Mark Mooibroek
  • 7,636
  • 3
  • 32
  • 53
  • I don't see how can you calculate the change of the 2 dates and get the format that user has confugured on his OS. – Sergiu Apr 05 '11 at 08:05
  • 2
    >< I misread your question. I think you should change your question :-) – Mark Mooibroek Apr 05 '11 at 08:21
  • I check the question and seems that toLocaleStrings is what he is looking for. Check the following description of this functions: `The toLocaleString method relies on the underlying operating system in formatting dates` [source](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/toLocaleString) – sanbor Oct 27 '11 at 13:03