date.toLocaleTimeString
does not work in chrome and always return time in 12 hours format. I need to display time on the basis of system's time format.
Asked
Active
Viewed 2,360 times
4

VincenzoC
- 30,117
- 12
- 90
- 112

Megha Jaiswal
- 550
- 5
- 11
-
Checking the system's time format might be a tedious approach, I'd rather use the `getTime()` to get the time stamp and display the time from that – Laazo Jul 21 '17 at 06:43
-
There is no ECMAScript interface to determine system settings such as the user system preferences for time formats and the format of *toString* and *toLocaleString* are entirely implementation dependent. So what you are asking is impossible to achieve reliably across even the most popular implementations. – RobG Jul 21 '17 at 06:48
-
2@Laazo—not so much tedious as impossible using ECMAScript alone. – RobG Jul 21 '17 at 06:50
-
you might want to throw moment-timezone into play and determine common 12/24 settings based on timezone. thats probably the best aproach unless you ask the user. – GottZ Jul 21 '17 at 07:01
-
I think that the title differ from the content of the question, I don't understand what you are trying to do. If you want to display time in different locales (and you can use external library) have a look to [i18n](http://momentjs.com/docs/#/i18n/) section of momentjs docs, maybe you are looking for [`format('LT')`](http://momentjs.com/docs/#/displaying/format/). Note that you have to _tell_ moment which locale you want to use. – VincenzoC Jul 21 '17 at 08:19
-
Its time format and not Locale. So, need to know current system (12/24 hour) format. – Megha Jaiswal Jul 21 '17 at 08:57
2 Answers
1
Short answer is No you can't get the default time format in the browser, because it relies on the System and on the browser settings, thought JavaScript doesn't have access for such options.
But if you want to manage the time format within your code, you can specify the format by which you wnat to show your Date.
Actually the toLocaleString() have a boolean hour12
option that tells the engine to use or not the 12 hours
format:
console.log(date.toLocaleString('en-US', { hour12: false }));
If you set it to false
it will display the time in 24 hours
format.

cнŝdk
- 31,391
- 7
- 56
- 78
0
Use
var x = date()
// Tue May 15 2012 05:45:40 GMT-0500
They already provide you the datetime with the timezone
OR
To make your life easier, use this
https://momentjs.com/

Raymond Seger
- 1,080
- 5
- 17
- 34
-
User can alter the time format in any Locale. i.e. We can have a time format as 24 hours in US locale. so, need to determine the current system settings. – Megha Jaiswal Jul 21 '17 at 08:59
-
the locale data cannot be set by the browser / javascript. It needs to be set by the server, and when you create / update date Objects through CRUD in the front-end, it must already have the UTC data probably as one hidden input HTML tag. – Raymond Seger Jul 21 '17 at 09:03
-
-
the locale data cannot be set by the browser / javascript. --Not required. Need to perform read operation. – Megha Jaiswal Jul 21 '17 at 09:20