有什么方法可以使用Moment.js解析时间字符串,但忽略时区信息? [英] Any way to parse a time string using Moment.js but ignore timezone info?
问题描述
考虑到时区问题的数量,我本以为能够找到该问题的答案,但没有成功.
Given the volume of Timezone questions, I would have thought to be able to find the answer to this issue, but haven't had any success.
有没有一种方法可以使用moment.js
解析ISO-8601字符串,但是可以在我的本地timzeone中对其进行解析?本质上,我想忽略ISO字符串中提供的时区信息.
Is there a way using moment.js
to parse an ISO-8601 string but have it parsed in my local timzeone? Essentially I want to ignore the timezone information that is supplied in the ISO string.
例如,如果我在EDT时区:
For example, if I am in EDT timezone:
var x = moment( "2012-12-31T00:00:00+0000" );
会给我: "2012-12-30T19:00:00-5000"
will give me: "2012-12-30T19:00:00-5000"
我希望忽略时区信息,只是给我一分钟相当于本地时间(2012-12-31T00:00:00-5000)的时间(EDT).
I'm looking to ignore the timezone info and just have it give me a moment equivalent of "2012-12-31T00:00:00-5000" local time (EDT).
推荐答案
我通过提供 format 作为第二个参数,并使用Moment的
I solved this by supplying a format as the second argument, and using Moment's method of escaping characters, and wrapped square brackets around the timezone.
moment("2016-01-01T05:00:00-05:00", "YYYY-MM-DDTHH:mm:ss[Z]").startOf("hour").format()
这仍将使用您的本地时区创建动量对象,但不会进行任何自动时区计算.因此,以上示例将为您提供凌晨5点(无论提供的时区如何).
This will still create moment objects using your local time zone, but it won't do any sort of auto-timezone calculation. So the above example will give you 5am regardless of timezone supplied.
这篇关于有什么方法可以使用Moment.js解析时间字符串,但忽略时区信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!