Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript toLocaleTimeString() not working

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?

like image 647
anh le Avatar asked Nov 12 '14 04:11

anh le


People also ask

What is toLocaleTimeString () in JavaScript?

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 .

How do I use toLocaleTimeString () without displaying seconds?

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'}) .

How do you get toLocaleString time?

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.


1 Answers

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.

like image 74
Chad Avatar answered Oct 06 '22 09:10

Chad