【发布时间】:2019-09-26 05:08:11
【问题描述】:
我想将日期时间从本地时间转换为 UTC,然后将该 UTC 时间解码回我原来的本地时间。
为了将本地时间转换为 UTC,我使用了以下代码,效果很好。
const now = new Date();
let d = Date.UTC(now.getFullYear(), now.getMonth(), now.getDate(), 13, 5, 0);
console.log(new Date(d).getHours(), new Date(d).getMinutes());
以上代码提供的 UTC 时间为 +5:30,输出为 18,35 (18:35:00)
在这里,为了将 18,35 (18:35:00) 转换回 13:05:00,我使用了以下代码
const now = new Date();
let d = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 18, 35, 0);
console.log(d.toUTCString());
上面的代码为我提供了一个字符串格式的日期,它的时间是 13:05:00,这是正确的。
现在,我想从这个日期字符串中获取小时和分钟。
我已尝试添加:
new Date(d.UTCString())
但是,它为我提供了 18 小时和 35 分钟,而我想要 13 小时和 05 分钟。 请帮我解决一下这个。谢谢。
【问题讨论】:
-
试试这个代码 -> console.log(d.getUTCHours(), d.getUTCMinutes());
-
UTC 不是一种格式。
标签: javascript date