【问题标题】:Get epoch for a specific date using Javascript使用 Javascript 获取特定日期的纪元
【发布时间】:2011-03-22 23:57:04
【问题描述】:

如何使用 Javascript 将07/26/2010 转换为 UNIX 时间戳?

【问题讨论】:

    标签: javascript javascript-framework


    【解决方案1】:

    Date.parse() 方法解析日期的字符串表示,并返回自January 1, 1970, 00:00:00 UTC 以来的毫秒数。

    const unixTimeZero = Date.parse('01 Jan 1970 00:00:00 GMT');
    const javaScriptRelease = Date.parse('04 Dec 1995 00:12:00 GMT');
    
    console.log(unixTimeZero);
    // expected output: 0
    
    console.log(javaScriptRelease);
    // expected output: 818035920000
    

    更多信息请访问:Date.parse()

    【讨论】:

      【解决方案2】:

      一些答案​​没有解释 JavaScript Date 对象时区变化的副作用。因此,如果您担心这个问题,您应该考虑这个答案。

      方法一:依赖机器的时区

      默认情况下,JavaScript 会根据机器的时区返回一个日期,因此 getTime() 的结果因计算机而异。您可以检查此行为是否正在运行:

      new Date(1970, 0, 1, 0, 0, 0, 0).getTime()
          // Since 1970-01-01 is Epoch, you may expect ZERO
          // but in fact the result varies based on computer's timezone
      

      如果您真的想要考虑您的时区的 Epoch 以来的时间,这不是问题。因此,如果您想获取当前日期或什至基于计算机时区的指定日期的 Epoch 以来的时间,您可以继续使用此方法。

      // Seconds since Epoch (Unix timestamp format)
      
      new Date().getTime() / 1000             // local Date/Time since Epoch in seconds
      new Date(2020, 11, 1).getTime() / 1000  // time since Epoch to 2020-12-01 00:00 (local timezone) in seconds
      
      // Milliseconds since Epoch (used by some systems, eg. JavaScript itself)
      
      new Date().getTime()                    // local Date/Time since Epoch in milliseconds
      new Date(2020,  0, 2).getTime()         // time since Epoch to 2020-01-02 00:00 (local timezone) in milliseconds
      
      // **Warning**: notice that MONTHS in JavaScript Dates starts in zero (0 = January, 11 = December)
      

      方法2:机器时区无关

      但是,如果您想了解时区的变化并获取自 Epoch 以来 UTC 中的指定日期 的时间(即与时区无关),您需要使用 Date.UTC 方法或将日期从您的时区转换为 UTC:

      Date.UTC(1970,  0, 1)
          // should be ZERO in any computer, since it is ZERO the difference from Epoch
      
          // Alternatively (if, for some reason, you do not want Date.UTC)
          const timezone_diff = new Date(1970, 0, 1).getTime()  // difference in milliseconds between your timezone and UTC
          (new Date(1970,  0, 1).getTime() - timezone_diff)
          // should be ZERO in any computer, since it is ZERO the difference from Epoch
      

      因此,使用这种方法(或者,减去差异),结果应该是:

      // Seconds since Epoch (Unix timestamp format)
      
      Date.UTC(2020,  0, 1) / 1000  // time since Epoch to 2020-01-01 00:00 UTC in seconds
      
          // Alternatively (if, for some reason, you do not want Date.UTC)
          const timezone_diff = new Date(1970, 0, 1).getTime()
          (new Date(2020,  0, 1).getTime() - timezone_diff) / 1000  // time since Epoch to 2020-01-01 00:00 UTC in seconds
          (new Date(2020, 11, 1).getTime() - timezone_diff) / 1000  // time since Epoch to 2020-12-01 00:00 UTC in seconds
      
      // Milliseconds since Epoch (used by some systems, eg. JavaScript itself)
      
      Date.UTC(2020,  0, 2)   // time since Epoch to 2020-01-02 00:00 UTC in milliseconds
      
          // Alternatively (if, for some reason, you do not want Date.UTC)
          const timezone_diff = new Date(1970, 0, 1).getTime()
          (new Date(2020,  0, 2).getTime() - timezone_diff)         // time since Epoch to 2020-01-02 00:00 UTC in milliseconds
      
      // **Warning**: notice that MONTHS in JavaScript Dates starts in zero (0 = January, 11 = December)
      

      IMO,除非您知道自己在做什么(参见上面的注释),否则您应该更喜欢 方法 2,因为它与机器无关。


      尾注

      尽管此答案中的建议,并且由于 Date.UTC 在没有指定日期/时间的情况下不起作用,但您可能倾向于使用替代方法并执行以下操作:

      const timezone_diff = new Date(1970, 0, 1).getTime()
      (new Date().getTime() - timezone_diff)  // <-- !!! new Date() without arguments
          // means "local Date/Time subtracted by timezone since Epoch" (?)
      

      这没有任何意义,可能是错误(您正在修改日期)。请注意不要这样做。如果您想从当前日期 AND TIME 获取自 Epoch 以来的时间,您很可能使用 方法 1

      【讨论】:

        【解决方案3】:
        Number(new Date(2010, 6, 26))
        

        工作方式与上述相同。如果你需要秒别忘了/ 1000

        【讨论】:

          【解决方案4】:

          你也可以使用 Date.now() 函数。

          【讨论】:

          • 技术上没有回答将 特定 日期转换为 seconds-since-the-epoch 的 OP 问题。但我喜欢简洁。显然Date.now() 在大多数情况下都比new Date().getTime() 好。
          【解决方案5】:
          new Date("2016-3-17").valueOf() 
          

          将返回一个较长的纪元

          【讨论】:

            【解决方案6】:

            您可以创建一个Date 对象,并在其上调用getTime

            new Date(2010, 6, 26).getTime() / 1000
            

            【讨论】:

            • 日期构造函数的月份参数缺乏一点一致性,实际上是从零开始的。这意味着 7 是八月,所以你需要减去 1 :-)
            • @Andy - 如果要使用 JavaScript,将几个月抵消 -1 并保留其余部分,这是一个古怪的想法,并且正是那种可以让航天飞机发疯的东西那里:)
            • 可能更好:new Date("2010-7-26").getTime() / 1000 所以你不必考虑偏移量。
            • @Vincent: new Date("2010-7-26") 不会在 Internet Explorer 中解析。 Date 构造函数在传递字符串时依赖于 Date.parse() 方法,该方法在 ECMA-262 第 3 版中依赖于实现,因此在即。
            • 也可以尽量减少指定时区的可能错误或误解。至少 Firefox、Opera 和 Chrome 的实际 (Linux-) 版本支持实例化 JavaScript 日期,例如 new Date("Jan 1, 1970 GMT+00:00, 00:01");这也减少了对正确月份格式的混淆。
            【解决方案7】:

            看看http://www.w3schools.com/jsref/jsref_obj_date.asp

            有一个函数 UTC() 可以返回 unix 纪元的毫秒数。

            【讨论】:

              猜你喜欢
              • 2018-05-18
              • 2018-10-31
              • 1970-01-01
              • 1970-01-01
              • 2021-07-03
              • 2019-04-15
              • 1970-01-01
              • 2017-08-22
              • 2011-04-08
              相关资源
              最近更新 更多