简体   繁体   English

如何使用moment.js和moment-timezone.js将特定时区(非本地)的给定日期/时间转换为UTC

[英]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.jsmoment-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.

相关问题 我需要使用 moment.js 根据本地时区转换 UTC 日期和时间,如何将日期从 UTC 转换为 html 中的时区 - I need to convert UTC date and time based on the local timezone using moment.js, how to convert date from UTC to timezone in html 使用moment.js和moment-timezone.js从不同时区的日期减去1天 - Subtract 1 day from date in different timezone using moment.js and moment-timezone.js 如何在本地时区中将UTC时间字符串转换为正确的moment.js对象? - How can I convert a UTC time string to the correct moment.js object in my local timezone? 如何将 Moment.js 日期转换为用户本地时区? - How to convert Moment.js date to users local timezone? 如何使用moment.js获得TimeZone明智的本地时间 - How to get TimeZone wise local time using moment.js moment.js和moment.timezone.js-我如何知道客户端时区以本地tz显示UTC日期? - moment.js, and moment.timezone.js - how do i know the client timezone to display UTC date in local tz? 如何使用Moment.js将本地时区偏移量应用于UTC日期时间? - How to apply the local timezone offset to a utc datetime with Moment.js? 使用时区将日期和时间字符串转换为moment.js - Convert date & time string to moment.js with timezone 如何将时区数据加载到Moment-timezone.js - How to load time zone data into Moment-timezone.js 使用moment.js从时区偏移量转换为本地时间 - converting to local time from timezone offset using moment.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM