本文介绍了jquery:如何从日期对象中获取 hh:mm:ss?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我怎样才能从日期对象中得到这个 hh:mm:ss?
How can I get this hh:mm:ss from date object?
var d = new Date(); // for now
datetext = d.getHours()+":"+d.getMinutes()+":"+d.getSeconds();
有时我会在下面得到这个结果,
I get this result below sometimes,
12:10:1
应该是
12:10:01
我认为这也会发生在小时和分钟上.
I assume this happens to the hour and minute as well.
所以我在追求这个
01:01:01
不是这个 1:1:1
推荐答案
Solution - (tl;dr version)
datetext = d.toTimeString().split(' ')[0]
说明:
toTimeString
返回完整时间.我们按空间分割它以仅获取时间分量,然后取第一个有用的值.:)
toTimeString
returns the complete time.We split it by space to get the time component only, then take the first value which is of use. :)
完整流程:
d = new Date();
// d is "Sun Oct 13 2013 20:32:01 GMT+0530 (India Standard Time)"
datetext = d.toTimeString();
// datestring is "20:32:01 GMT+0530 (India Standard Time)"
// Split with ' ' and we get: ["20:32:01", "GMT+0530", "(India", "Standard", "Time)"]
// Take the first value from array :)
datetext = datetext.split(' ')[0];
注意:这不需要您包含任何外部文件或库,因此执行所需的时间会更快.
Note: This does not require you to include any external files or libraries hence time required for execution would be faster.
这篇关于jquery:如何从日期对象中获取 hh:mm:ss?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!