简体   繁体   English

Javascript toLocaleTimeString()不起作用

[英]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 我的时区是(UTC-10:00) Hawaii

date.toString(); displays correctly, but toLocaleTimeString() , toLocaleDateString() displays incorrect output 正确显示,但toLocaleTimeString()toLocaleDateString()显示不正确的输出

How do I fix this issue? 我该如何解决这个问题?

You can provide the time zone in the options parameter to toLocaleTimeString() : 您可以在options参数中为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. 必须从IANA时区数据库中指定时区,这有点奇怪......但它确实有效。

Use it like so: 像这样使用它:

dateObj.toLocaleTimeString([locales[, options]])

console.log(date.toLocaleTimeString('en-US'));

Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleTimeString 参考: https//developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleTimeString

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM