【发布时间】:2019-12-04 05:10:54
【问题描述】:
我从天气 api 获得了纽约的时间戳 UTC,想要显示纽约的当前时间,但它给出的输出类似于“UTC Sun Dec 01 2019 05:00:00 GMT+0530 (India Standard Time)”。 参考代码见
// Code 1
//I get timestamp_utc when console.log(data)
//timestamp_utc: "2019-12-01T05:00:00"
const utc = new Date(data.timestamp_utc)
console.log('UTC', utc)
// UTC Sun Dec 01 2019 05:00:00 GMT+0530 (India Standard Time)
// Code 2
// Another code for getting current time but, failed
var usaTime = new Date().toLocaleString("en-US", {timeZone: timezone}); // Here timezone is from props
console.log('USA time: '+usaTime) // USA time: 12/1/2019, 4:59:58 AM
我也有从天气 API 获取的时区数据。我的目标是根据时区或 utc 时间戳获取当前时间。正如你所看到的,我的两次试验都没有成功。预计输出时间为下午 6:39,现在是纽约的当前时间。有什么好的解决办法吗?
【问题讨论】:
标签: javascript datetime timezone