Hello I have a piece of code
var date = new Date("11/12/2014 02:58:11 UTC");
console.info(date.toString());
console.info(date.toLocaleTimeString());
console.info(date.toLocaleDateString());
and display:
Tue Nov 11 2014 16:58:11 GMT-1000 (Hawaiian Standard Time)
9:58:11 AM
11/12/2014
My time zone is (UTC-10:00) Hawaii
date.toString();
displays correctly, but toLocaleTimeString()
, toLocaleDateString()
displays incorrect output
How do I fix this issue?
The toLocaleTimeString() method returns a string with a language-sensitive representation of the time portion of the date. In implementations with Intl. DateTimeFormat API support, this method simply calls Intl. DateTimeFormat .
To use the toLocaleTimeString() method without displaying seconds, set the hour and minute parameters in the method's options object, e.g. date. toLocaleTimeString([], {hour: '2-digit', minute: '2-digit'}) .
To get the current date and time in JavaScript, you can use the toLocaleString() method, which returns a string representing the given date according to language-specific conventions. To display only the time, you can use the toLocaleTimeString() method. If you're already using the Moment.
You can provide the time zone in the options
parameter to toLocaleTimeString()
:
date.toLocaleTimeString('en-US',{timeZone:'America/Adak'})
The time zone has to be specified from the IANA time zone database, which is a little odd... but it works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With