【发布时间】:2021-12-19 09:31:18
【问题描述】:
在 Javascript 中,我想计算今天的日期(但已调整为 GMT0)与给定日期 1970/01/01 之间的差异(以毫秒为单位)。
例如在 powershell 上,这将是 (([datetime]::UtcNow) - (get-date "1/1/1970")).TotalMilliseconds
我尝试像这样使用 moment.js:
A = moment.utc();
B = moment('19700101', 'YYYYMMDD')
console.log("A is ",A)
console.log("B is ",B)
console.log("A format is ", A.format())
console.log("B format is ", B.format())
console.log("Milisec diff : ", A.diff(B,'miliseconds'))
返回
A is h {_isAMomentObject: true, _useUTC: true, _isUTC: true, _l: undefined, _i: undefined, …}_d: Fri Nov 05 2021 10:43:43 GMT+0100 (heure normale d’Europe centrale) {}_f: undefined_i: undefined_isAMomentObject: true_isUTC: true_isValid: true_l: undefined_offset: 0_pf: {empty: false, unusedTokens: Array(0), unusedInput: Array(0), overflow: -2, charsLeftOver: 0, …}_strict: undefined_useUTC: true_z: null[[Prototype]]: Object
B is h {_isAMomentObject: true, _i: '19700101', _f: 'YYYYMMDD', _l: undefined, _strict: undefined, …}_a: (7) [1970, 0, 1, 0, 0, 0, 0]_d: Thu Jan 01 1970 00:00:00 GMT+0100 (heure normale d’Europe centrale) {}_f: "YYYYMMDD"_i: "19700101"_isAMomentObject: true_isUTC: false_isValid: true_l: undefined_pf: {empty: false, unusedTokens: Array(0), unusedInput: Array(0), overflow: -1, charsLeftOver: 0, …}_strict: undefined[[Prototype]]: Object
A format is 2021-11-05T09:43:43+00:00
B format is 1970-01-01T00:00:00+01:00
Milisec diff : 1636109023628
所以差异不是很好,它需要 A 与当地时间(对我来说是 10:43)而不是 UTC 时间(9:43)。这可能是因为您必须考虑 A.format 才能看到 9:43,但我们不能将 A.format 赋予 diff 函数。
感谢您的帮助!
【问题讨论】:
标签: javascript time momentjs utc gmt