Javascript 将日期和时间转换为 UTC

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37117374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 19:56:52  来源:igfitidea点击:

Convert date and time to UTC

javascriptdatetimemomentjsdatetime-format

提问by erichardson30

I am asking the user to input a date and time into my application. The user will input a time based on the timezone they are in. When I save this date and time to my database I want to convert the time to UTC so that when I query by time (which is done in UTC) I can find the entries.

我要求用户在我的应用程序中输入日期和时间。用户将根据他们所在的时区输入时间。当我将此日期和时间保存到我的数据库时,我想将时间转换为 UTC,以便当我按时间查询(以 UTC 完成)时,我可以找到条目。

This is what I've currently done :

这是我目前所做的:

var date = new Date();
var dateString = "0" + (date.getMonth() + 1) + "/" + date.getDate() + "/" + date.getFullYear() + " " + time;
//format date
date = moment(dateString, "MM/DD/YYYY HH:mm a");
date = new Date(date).toISOString();

Where time is the time the user enters (ex if I want to schedule something for 11:00am, time = 11:00am)

time 是用户进入的时间(例如,如果我想在上午 11:00 安排某事,则时间 = 11:00 am)

When this is saved to the database, it looks like : ISODate("2016-05-09T11:00:00Z")which is not correct since that is a EST saved as Zulu time.

当它保存到数据库时,它看起来像: ISODate("2016-05-09T11:00:00Z")这是不正确的,因为这是保存为祖鲁时间的 EST。

How can I convert the time (I am using moment) to be saved as the correct Zulu time?

如何将时间(我正在使用时刻)转换为正确的祖鲁时间?

回答by WonderGrub

One option would be to use Javascript's built-in UTC functions.

一种选择是使用 Javascript 的内置 UTC 函数。

JavaScript Date Reference

JavaScript 日期参考

getUTCDate() - Returns the day of the month, according to universal time (from 1-31)
getUTCDay() - Returns the day of the week, according to universal time (from 0-6)
getUTCFullYear()- Returns the year, according to universal time
getUTCHours() - Returns the hour, according to universal time (from 0-23)
getUTCMilliseconds() - Returns the milliseconds, according to universal time (from 0-999)
getUTCMinutes() - Returns the minutes, according to universal time (from 0-59)
getUTCMonth() - Returns the month, according to universal time (from 0-11)
getUTCSeconds() - Returns the seconds, according to universal time (from 0-59)

For example,

例如,

new Date('2016-05-09 10:00:00')
    returns Mon May 09 2016 10:00:00 GMT-0400

new Date('2016-05-09 10:00:00').getUTCHours()
    returns 14

UPDATE: Examples (including .toISOString())

更新:示例(包括 .toISOString())

If we choose July 4, 2016 @ 8:00 PM Eastern Time (GMT-0400), UTC would be July 5, 2016 @ 00:00 (midnight):

如果我们选择东部时间 2016 年 7 月 4 日晚上 8:00 (GMT-0400),UTC 将是 2016 年 7 月 5 日 @ 00:00(午夜):

var date = new Date('2016-07-04 20:00:00')
date.getUTCFullYear = 2016
date.getUTCMonth = 6 (0 base)
date.getUTCDate = 5
date.getUTCHours = 0
date.getUTCMinutes = 0

var date = new Date('2016-07-04 20:00:00')
date.toISOString() = "2016-07-05T00:00:00.000Z" (UTC)

回答by erichardson30

To fix this issue I used the moment-timezone library.

为了解决这个问题,我使用了 moment-timezone 库。

I first set the timezone on the server since it will only be accessed by EST :

我首先在服务器上设置时区,因为它只能由 EST 访问:

moment.tz.setDefault("America/New_York");

Then all I needed to do was set the timezone on the moment object to UTC by :

然后我需要做的就是通过以下方式将时刻对象上的时区设置为 UTC:

date = moment(dateString, "MM/DD/YYYY HH:mm a").tz("UTC");

This successfully converts the time from EST to UTC

这成功地将时间从 EST 转换为 UTC