【发布时间】:2021-10-06 00:46:00
【问题描述】:
我有一个有时间的约会,像这样
const date = {year: 2020, month: 12, day: 31};
const time = {hours: 16, minutes: 2};
如何根据时区获取该时间的 UTC 表示? (不使用任何库)
convetToUTC(date, time, "Europe/Moscow") // => <UTC timestamp>
convetToUTC(date, time, "America/New_York") // => <UTC timestamp>
例子
convetToUTC(
{year: 2021, month: 7, day: 30},
{hours: 16, minutes: 15},
"Europe/Moscow"
) // => 1627650900
convetToUTC(
{year: 2021, month: 7, day: 30},
{hours: 16, minutes: 15},
"America/New_York"
) // => 1627676100
【问题讨论】:
-
请查看
https://stackoverflow.com/questions/9756120/how-do-i-get-a-utc-timestamp-in-javascript -
检查,看不到任何提示如何进行任意时区转换
-
“我如何根据时区获得该时间的 UTC 表示。”这是否意味着您要获取指定的日期和时间,将其视为指定时区的日期和时间,然后获取该时刻的 UTC 时间戳?
-
x = new Date() var UTCseconds = (x.getTime() + x.getTimezoneOffset()*60*1000)/1000; console.log("UTCseconds", UTCseconds)
-
@Forest1 这不适用于本地时区以外的任何地方。 OP 希望为任意时区做到这一点。
标签: javascript date datetime