计算 Javascript 中的 utc 日期和给定日期之间的时差
Calculate time difference between utc date and given date in Javascript
在 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'))
它returns
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
所以 差异不是很好,它需要本地时间(我是 10:43)而不是 UTC 时间(9:43) .这可能是因为你必须考虑 A.format 才能看到 9:43 但我们不能将 A.format 给 diff 函数。
感谢您的帮助!
对上面的代码稍作改动,应该会得到您想要的结果:
A = moment.utc();
B = moment.utc(0)
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'))
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" integrity="sha512-qTXRIMyZIFb8iQcfjXWCO8+M5Tbc38Qi5WzdPOYZHIlZpzBHG3L3by84BBBOiRGiEb7KKtAOAs5qYdUiZiQNNQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
您还可以使用带有 UTC 时区 ('Z') 的 ISO 8601 时间戳,如下所示:
A = moment.utc();
B = moment('1970-01-01T00:00:00Z')
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'))
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" integrity="sha512-qTXRIMyZIFb8iQcfjXWCO8+M5Tbc38Qi5WzdPOYZHIlZpzBHG3L3by84BBBOiRGiEb7KKtAOAs5qYdUiZiQNNQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
使用本机 JS Date 对象的更简单方法是简单地使用 Date.now(),这将为您提供自 1970 年 1 月 1 日以来的毫秒数 00:00:00 UTC。
console.log("Milisec diff : ", Date.now())
在 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'))
它returns
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
所以 差异不是很好,它需要本地时间(我是 10:43)而不是 UTC 时间(9:43) .这可能是因为你必须考虑 A.format 才能看到 9:43 但我们不能将 A.format 给 diff 函数。
感谢您的帮助!
对上面的代码稍作改动,应该会得到您想要的结果:
A = moment.utc();
B = moment.utc(0)
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'))
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" integrity="sha512-qTXRIMyZIFb8iQcfjXWCO8+M5Tbc38Qi5WzdPOYZHIlZpzBHG3L3by84BBBOiRGiEb7KKtAOAs5qYdUiZiQNNQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
您还可以使用带有 UTC 时区 ('Z') 的 ISO 8601 时间戳,如下所示:
A = moment.utc();
B = moment('1970-01-01T00:00:00Z')
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'))
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" integrity="sha512-qTXRIMyZIFb8iQcfjXWCO8+M5Tbc38Qi5WzdPOYZHIlZpzBHG3L3by84BBBOiRGiEb7KKtAOAs5qYdUiZiQNNQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
使用本机 JS Date 对象的更简单方法是简单地使用 Date.now(),这将为您提供自 1970 年 1 月 1 日以来的毫秒数 00:00:00 UTC。
console.log("Milisec diff : ", Date.now())