[英]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.js和moment时区库,但我不确定如何利用这些来完成这项任务。
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.