简体   繁体   English

javascript:将日期和时间字符串转换为UTC

[英]javascript: convert date and time strings to UTC

I have had a truly gnarly time trying to work with Date objects in javascript, which strike me as ugly and unintuitive. 我有一个真正粗糙的时间试图在javascript中使用Date对象,这让我感到丑陋和不直观。

I am getting back two objects from an internal API: one date string in the form of YYYY-MM-DD and one time string in the form of HH:MM . 我从内部API返回两个对象:一个YYYY-MM-DD形式的date字符串和一个HH:MM形式的time字符串。

What I want to do, in javascript, is to merge these into a UTC-formatted string of the form YYYY-MM-DDTHH:MM:00Z . 我想在javascript中做的是将它们合并为格式为YYYY-MM-DDTHH:MM:00Z的UTC格式的字符串。 I have been playing around a bit with the moment.js and moment timezone libraries, but I'm not sure how I might leverage these to complete this task. 我一直在玩moment.jsmoment时区库,但我不确定如何利用这些来完成这项任务。

I solved this thusly using moment timezone: 我使用时刻时区解决了这个问题:

dateTime = date.concat(" ").time
dateTimeAdjusted = moment.tz(dateTime, timezone).format()

(where timezone was a variable that I had access to that contained the timezone of the given date and time.) (其中timezone是我可以访问的变量,其中包含给定日期和时间的时区。)

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

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