简体   繁体   English

在不更改日期本身的情况下将日期的时区更改为 UTC

[英]Change a date's timezone to UTC without changing the date itself

Say if the date is:说如果日期是:

var now = new Date(); // Tue Apr 19 2022 13:12:41 GMT+0530

I need to change only the timezone to UTC.我只需要将时区更改为 UTC。 so Tue Apr 19 2022 13:12:41 GMT+0000 in above case.所以在上述情况下, Tue Apr 19 2022 13:12:41 GMT+0000 Things like toISOString() gives actual UTC ie modified date value after adjusting the offset.诸如toISOString()之类的东西会在调整偏移量后给出实际的 UTC,即修改后的日期值。

You can simply add the timezone difference:您可以简单地添加时区差异:

let x = new Date()
x.setMinutes(x.getMinutes() - x.getTimezoneOffset())

You're basically asking for this:你基本上是在要求这个:

 const now = new Date; const utc = new Date(Date.UTC(now.getFullYear(), now.getMonth(), now.getDate(), now.getHours(), now.getMinutes(), now.getSeconds())); console.log(now, utc);

This creates a date in your local timezone, and then another date with the same year, month, day, hour, minute and second value , but in the UTC timezone.这会在您的本地时区创建一个日期,然后在 UTC 时区创建另一个具有相同年、月、日、小时、分钟和秒的日期。

What sense that makes is another topic…这有什么意义是另一个话题......

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

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