从 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
谁能告诉我为什么这些日期函数不起作用?我想获取一个日期字符串并将其转换为 UTC 毫秒。如您所见,我将字符串传递给 Date 构造函数,然后将函数 getUTCMilliseconds() 应用于返回的日期对象。为什么它返回零??
最佳答案
结果是正确的——对函数名称的理解是错误的(我也是)。
Date.getUTCMilliseconds()
被定义为返回日期的毫秒部分,其方式与 getMinutes()
返回存储在对象中的分钟数相同(在您的示例中,将返回 48 分钟) .
澄清一下,关于您的日期[2013-04-14 11:48]
,各个部分是:
getFullYear()
=== 2013getDate()
=== 14getSeconds()
=== 0(因为您的日期字符串定义了整数分钟)getMilliseconds()
=== 0(同理)
一个反例可能是[2017-11-15 16:53:10.78]
:
getSeconds()
=== 10getMilliseconds()
=== 78
The functions on Date
are laid out nicely on the W3Schools page here.
我看起来你在追求 Unix 时间戳值(这是 Google 带我来这里时我想要的)。
-
Date.getTime()
将返回自 1970/01/01 以来的毫秒数
幸运的是,这是当前实现中的基础值,which is why the other answer works so nicely .
关于javascript - 时间日期对象 JavaScript getUTCMilliseconds,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29143405/