在javascript / momentjs中创建日期时忽略时区 [英] Ignoring timezones when creating a date in javascript/momentjs
问题描述
我正在为东海岸的某个东西创建一个时间表,我需要创建一个总是返回此时的日期对象/时间戳的日期。
I am creating a schedule for something on the east coast, and I need to create a date that always returns a date object/timestamp for this time.
所以,如果我在18:00在巴黎打开页面,new myDate()
应该返回12:00。
So, if I open the page in Paris at 18:00, the "new myDate()"
should return 12:00.
关于如何使用momentJS或Javascript进行此操作的任何建议?最终结果应该是一个javascript Date对象。
Any suggestions on how I can do this with wither momentJS or just Javascript? The end result should be a javascript Date object.
推荐答案
你可以使用对moment.js执行此操作moment.utc()
。
http://momentjs.com/docs/#/parsing/utc/
moment([2011, 10, 8, 5]).format(); // different output based on timezone
moment.utc([2011, 10, 8, 5]).format(); // same output for all timezones
方式 moment.utc
工作是通过在内部设置一个标志来使用 getUTCMinutes
而不是 getMinutes
,因此输出是相同的在所有时区。
The way moment.utc
works is by setting a flag internally to use getUTCMinutes
instead of getMinutes
, thus the output is the same in all timezones.
这篇关于在javascript / momentjs中创建日期时忽略时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!