【发布时间】:2018-03-03 11:43:01
【问题描述】:
我的时刻对象不会转换时区。我正在尝试将日期和时间对象转换为 UTC,但它只是返回原始日期/时间而没有任何变化。有谁知道为什么会这样?看起来它可能会假设原始时间为 UTC,即使它不是根据 _d 的?
momentPre = moment(post.date + " " + post.time + "00", "YYYY-M-D HH:mm:ss")
momentAft = momentPre.toISOString()
momentParse = moment.tz(momentAft, 'America/Denver')
post.utcDate = momentParse.clone().tz("UTC")._i.substring(0,10)
momentParse 对象输出(_isUTC 返回 true,即使它不是。我在测试之前已将其强制为 false 并且没有解决问题):
{ [Number: 1506010200000]
_isAMomentObject: true,
_i: '2017-09-21T16:10:00.000Z',
_f: 'YYYY-MM-DDTHH:mm:ss.SSSSZ',
_tzm: 0,
_isUTC: true,
_pf:
{ empty: false,
unusedTokens: [],
unusedInput: [],
overflow: -1,
charsLeftOver: 0,
nullInput: false,
invalidMonth: null,
invalidFormat: false,
userInvalidated: false,
iso: true },
_locale: Locale { ordinal: [Function], _abbr: 'en' },
_a: [ 2017, 8, 21, 16, 10, 0, 0 ],
_d: Thu Sep 21 2017 04:10:00 GMT-0600 (MDT),
_z:
{ name: 'America/Denver',
abbrs
...
momentParse.clone().tz("UTC") 的输出
{ [Number: 1506010200000]
_isAMomentObject: true,
_i: '2017-09-21T16:10:00.000Z',
_f: 'YYYY-MM-DDTHH:mm:ss.SSSSZ',
_tzm: 0,
_isUTC: true,
_offset: 0,
_pf:
{ empty: false,
unusedTokens: [],
unusedInput: [],
overflow: -1,
charsLeftOver: 0,
nullInput: false,
invalidMonth: null,
invalidFormat: false,
userInvalidated: false,
iso: true },
_locale: Locale { ordinal: [Function], _abbr: 'en' },
_z:
{ name: 'UTC',
abbrs: [ 'UTC' ],
untils: [ Infinity ],
offsets: [ 0 ] },
_a: [ 2017, 8, 21, 16, 10, 0, 0 ],
_d: Thu Sep 21 2017 10:10:00 GMT-0600 (MDT) }
【问题讨论】:
-
你添加了时刻时区js文件
-
是的,我正在使用 mrt:moment-timezone 包。
-
我不能从你的问题中完全确定你想要在用例方面实现什么以及你为什么需要它。在 Meteor Collection 中保存任何字段时,默认保存为 UTC。在客户端可以感受到差异。客户根据他们的时区将能够正确读取日期。那么,为什么需要时区转换?如果你真的需要它,你能在问题中解释为什么吗?
-
How to create a Minimal, Complete, and Verifiable example。你应该展示你如何打电话,你期望什么以及你得到什么。否则,真的很难理解你的问题是什么。
标签: javascript meteor momentjs