use*_*858 3 javascript jquery html5 datetime
我有一个像“ 2015-09-30T17:32:29.000-05:00”这样的日期值,当我从如下所示的该日期获取getMilliseconds时,我只是得到0而没有得到000。为什么?我想得到三位数的毫秒?
myDate =2015-09-30T17:33:28.000-04:00;
var msecs = myDate.getMilliseconds()
Run Code Online (Sandbox Code Playgroud)
我得到的毫秒数= 0。我想将毫秒作为000。我如何实现这一目标?
您可以用两个零填充毫秒数(以确保至少有三个数字),然后使用String.prototype.slice()来仅获取最后三个字符(数字)。
var msecs = ('00' + myDate.getMilliseconds()).slice(-3);
Run Code Online (Sandbox Code Playgroud)
这样,即使返回的毫秒数已经是三位数长,当您slice()将字符串-3作为参数传递时,作为填充添加的零也会被剥离:
// when the number of milliseconds is 123:
myDate.getMilliseconds() === 123
'00' + myDate.getMilliseconds() === '00123'
('00' + myDate.getMilliseconds()).slice(-3) === '123'
// or, when the number is 0:
myDate.getMilliseconds() === 0
'00' + myDate.getMilliseconds() === '000'
('00' + myDate.getMilliseconds()).slice(-3) === '000'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
795 次 |
| 最近记录: |