【问题标题】:Calculate time difference between utc date and given date in Javascript在Javascript中计算UTC日期和给定日期之间的时间差
【发布时间】: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


    【解决方案1】:

    对上面的代码稍加改动应该会得到你想要的结果:

    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.now(),这将为您提供自 1970-01-01 00:00:00 UTC 以来的毫秒数。

    console.log("Milisec diff : ", Date.now())

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-14
      • 2016-08-14
      • 2019-01-24
      • 2010-11-07
      相关资源
      最近更新 更多