Reputation: 201
I have a requirement in my project to show a date in the client machine format. I am aware of the property in javascript window.navigator.userLanguage or window.navigator.language.. But it is only returning the language of the client machine like 'en-US', 'en-GB' etc.
If the user customizes the date format of the machine for eg:-dd-MMM-yyyy, is there any way to get that format in Javascript?
Thanks in advance
suhaib
Upvotes: 1
Views: 4570
Reputation: 150030
You can use the .toLocaleDateString()
method:
var yourDate = new Date();
alert(yourDate.toLocaleDateString());
This doesn't tell your code what the user's selected date format is, but it lets you display a date in whatever the user's format is.
On my PC the above alerts "Tuesday, 26 June 2012".
The .toLocaleTimeString()
method does the equivalent thing for the time.
The .toLocaleString()
method displays date and time.
Upvotes: 2