【问题标题】:Time Date Objects JavaScript getUTCMilliseconds时间日期对象 JavaScript getUTCMilliseconds
【发布时间】:2015-03-19 11:28:48
【问题描述】:

从 Chrome 控制台:

Me:    var dateObj = new Date("2013-04-14 11:48");
undefined
Me:    dateObj
Sun Apr 14 2013 11:48:00 GMT+0200 (Central Europe Daylight Time)
Me:    dateObj.getUTCMilliseconds();
0

谁能告诉我为什么这些 Date 函数不起作用?我想获取一个日期字符串并将其转换为 UTC 毫秒。如您所见,我将字符串传递给 Date 构造函数,然后将函数 getUTCMilliseconds() 应用于返回的日期对象。为什么返回零??

【问题讨论】:

  • 你可能想要getTime。
  • new Date().getUTCMilliseconds() 还返回一个无意义的答案(整数值以数百而不是万亿)

标签: javascript date time utc


【解决方案1】:

结果是正确的——对函数名的理解是错误的(和我的一样)。

Date.getUTCMilliseconds() 被定义为返回日期的毫秒部分,就像getMinutes() 返回存储在对象中的分钟一样(在您的示例中,这将返回 48 分钟)。


澄清一下,关于您的日期[2013-04-14 11:48],各个部分是:

  • getFullYear() === 2013
  • getDate() === 14
  • getSeconds() === 0(因为您的日期字符串定义了整数分钟数)
  • getMilliseconds() === 0(出于同样的原因)

反例可能是[2017-11-15 16:53:10.78]:

  • getSeconds() === 10
  • getMilliseconds() === 78

The functions on Date are laid out nicely on the W3Schools page here.


我看起来你是在 Unix 时间戳值e 之后(当 Google 把我带到这里时我想要的)。

幸运的是,这是当前实现中的基础值,which is why the other answer works so nicely

【讨论】:

    【解决方案2】:

    你可以使用JavaScript Date valueOf() Method

    返回 Date 对象的原始值:

    dateObj.valueOf()
    1365929280000
    

    【讨论】:

      猜你喜欢
      • 2012-10-08
      • 1970-01-01
      • 1970-01-01
      • 2014-05-26
      • 2017-12-19
      • 2017-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多