将本地时间转换为 utc 并添加到 utc 日期时间时刻
convert local time to utc and add to utc datetime moment
我有一个用户输入,用户在其中输入年、月和日,然后我创建了一个日期对象
let userDate = new Date(year, month, day)
然后从用户输入分钟和小时(进入变量分钟和小时)
我需要使用 moment 将此日期和时间转换为 UTC 时间戳,但我不确定。我可以做吗
let utcTimestamp = moment.utc(date.toISOString()).hour(hours).minutes(minutes);
例如:如果用户输入日期 2018 年 3 月 13 日和时间 8:45 AM(在 GMT 时区),那么我可以使用上面的代码行来获取 UTC 时间戳,因为我可以直接在日期上加上小时和分钟
如果用户输入日期 2018 年 8 月 13 日和时间 8:45(由于夏令时更改,格林威治标准时间 +1),那么我可能会在上面一行中创建错误的日期。
... I create a date object
let userDate = new Date(year, month, day)
这里要小心,您需要从月份中减去 1,因为 Date
将它们编号为 0-11,而不是通常的 1-12。
然后从用户输入分钟和小时(进入变量分钟和小时)
I need to convert this date and time into UTC timestamp, using moment ...
您不需要 Moment。只需在 Date
对象上调用 .toISOString()
。它在生成字符串之前隐式转换为 UTC。
var utcString = new Date(year, month-1, day, hours, minutes).toISOString();
... Can I just do
let utcTimestamp = moment.utc(date.toISOString()).hour(hours).minutes(minutes);
不,那没有意义。只有在以本地时间输入日期但以 UTC 时间输入时间的情况下,这才有效。这种行为上的差异肯定会让您的用户感到困惑 - 特别是如果 UTC 日期与本地日期不同。
... if a user enters a date of 13-Aug-2018 and time 8:45 (which is GMT +1, due to daylight savings time change) then with above line I might be creating a wrong date.
从当地时间到 UTC 的转换将自动考虑当地时区正在观察的任何夏令时调整。您不需要自己做任何额外的调整。
这是我用来将 Outlook 日历事件从 UTC 时间转换为本地时间的片段。相同的技术可以用于其他场景。
//Get the users timezone
let timeZone = item.originalStartTimeZone;
//Get the start datetime in UTC time
let timeStart = item.start.dateTime;
//Calculate the negative offset.
let offset = - timeStart.localeCompare(timeZone);
//Add the calculated offset to the UTC start time
let localStartTime = addHours(timeStart, offset);
我有一个用户输入,用户在其中输入年、月和日,然后我创建了一个日期对象
let userDate = new Date(year, month, day)
然后从用户输入分钟和小时(进入变量分钟和小时)
我需要使用 moment 将此日期和时间转换为 UTC 时间戳,但我不确定。我可以做吗
let utcTimestamp = moment.utc(date.toISOString()).hour(hours).minutes(minutes);
例如:如果用户输入日期 2018 年 3 月 13 日和时间 8:45 AM(在 GMT 时区),那么我可以使用上面的代码行来获取 UTC 时间戳,因为我可以直接在日期上加上小时和分钟
如果用户输入日期 2018 年 8 月 13 日和时间 8:45(由于夏令时更改,格林威治标准时间 +1),那么我可能会在上面一行中创建错误的日期。
... I create a date object
let userDate = new Date(year, month, day)
这里要小心,您需要从月份中减去 1,因为 Date
将它们编号为 0-11,而不是通常的 1-12。
然后从用户输入分钟和小时(进入变量分钟和小时)
I need to convert this date and time into UTC timestamp, using moment ...
您不需要 Moment。只需在 Date
对象上调用 .toISOString()
。它在生成字符串之前隐式转换为 UTC。
var utcString = new Date(year, month-1, day, hours, minutes).toISOString();
... Can I just do
let utcTimestamp = moment.utc(date.toISOString()).hour(hours).minutes(minutes);
不,那没有意义。只有在以本地时间输入日期但以 UTC 时间输入时间的情况下,这才有效。这种行为上的差异肯定会让您的用户感到困惑 - 特别是如果 UTC 日期与本地日期不同。
... if a user enters a date of 13-Aug-2018 and time 8:45 (which is GMT +1, due to daylight savings time change) then with above line I might be creating a wrong date.
从当地时间到 UTC 的转换将自动考虑当地时区正在观察的任何夏令时调整。您不需要自己做任何额外的调整。
这是我用来将 Outlook 日历事件从 UTC 时间转换为本地时间的片段。相同的技术可以用于其他场景。
//Get the users timezone
let timeZone = item.originalStartTimeZone;
//Get the start datetime in UTC time
let timeStart = item.start.dateTime;
//Calculate the negative offset.
let offset = - timeStart.localeCompare(timeZone);
//Add the calculated offset to the UTC start time
let localStartTime = addHours(timeStart, offset);