[英]How to convert a given date/time for a specific timezone (not local) to UTC using moment.js and moment-timezone.js
How can II want to convert a given date/time for a specific timezone (not local) to UTC using moment.js
and moment-timezone.js
II如何使用
moment.js
和moment-timezone.js
将特定时区(非本地)的给定日期/时间转换为UTC
I use: 我用:
var s = moment("10/15/2014 09:25 AM").tz("America/Los_Angeles").format('hh:mm:ss a');
I have a difficulty when I want to give a value on moment()
. 当我想给
moment()
赋值时,我遇到了困难。
Let me explain you the facts: 让我向您解释事实:
I have to take a date/time value from a cell with the following format: 10/15/2014 09:25 AM (MM/DD/YYYY h:mm a)
. 我必须从具有以下格式的单元格中获取日期/时间值:
10/15/2014 09:25 AM (MM/DD/YYYY h:mm a)
。 This value is not a constant one, its the opened time for some entries. 该值不是一个常数,它是某些条目的打开时间。 I want to transform this string in UTC.
我想在UTC中转换此字符串。 Unfortunately, the string is not my local time, is in
America/Los_Angeles (PDT/PST)
timezone. 不幸的是,该字符串不是我的本地时间,而是在
America/Los_Angeles (PDT/PST)
时区。 I want also to take care automatically about PDT(9 Mar, 2 Nov)
and PST
. 我还要自动注意
PDT(9 Mar, 2 Nov)
和PST
。
-10/15/2014 09:25 AM America/Los_Angeles -07:00 => 10/15/2014 04:25 PM UTC 00:00
-12/15/2014 09:25 AM America/Los_Angeles -08:00 => 12/15/2014 05:25 PM UTC 00:00
How can I do this? 我怎样才能做到这一点?
If I use: 如果我使用:
Var s = moment("10/15/2014 09:37 PM").tz("America/Los_Angeles").format('hh:mm:ss a');
...it will be parsed as my local time and it will be converted to America/Los_Angeles
. ...将被解析为我的本地时间,并将其转换为
America/Los_Angeles
。
- 10/15/2014 09:25 AM Eastern European Time +03:00 => 10/15/2014 11:25 PM America/Los_Angeles +07:00
I think that a short description for my problem is this: 我认为对我的问题的简短描述是这样的:
- How can I "tell" to script that this string 10/15/2014 09:37 AM
is from a specific timezone. -如何“告诉”脚本该字符串
10/15/2014 09:37 AM
来自特定时区。 After this, the conversion to UTC
is piece of cake. 在此之后,转换为
UTC
就是小菜一碟。
Thanks. 谢谢。
You must provide a pattern when you want to parse a date string with a specific timezone, except for UTC. 要解析具有特定时区的日期字符串(UTC除外)时, 必须提供一种模式。 For UTC you can just provide the date string.
对于UTC,您只需提供日期字符串即可。
Usage: moment.tz(string, pattern, zoneString) 用法:moment.tz(字符串,模式,zoneString)
In your case: 在您的情况下:
var moment = require("moment-timezone");
var d1 = moment.tz("10/15/2014 09:25 AM", "MM/DD//YYYY hh:mm A", "America/Los_Angeles");
d1.toString(); // Wed Oct 15 2014 09:25:00 GMT-0700
d1.tz("UTC").format('hh:mm:ss a'); // '04:25:00 pm'
If you omit the formatter the parsing is wrong, but there is a deprecated note: https://github.com/moment/moment/issues/1407 如果省略格式化程序,则解析是错误的,但是有不建议使用的注释: https : //github.com/moment/moment/issues/1407
Read this http://momentjs.com/timezone/docs/#/using-timezones/parsing-ambiguous-inputs/ to handle DST 阅读此http://momentjs.com/timezone/docs/#/using-timezones/parsing-ambiguous-inputs/以处理DST
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.