jquery:如何从日期对象中获取 hh:mm:ss?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19346405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery: How to get hh:mm:ss from date object?
提问by laukok
How can I get this hh:mm:ss from date object?
我怎样才能从日期对象中得到这个 hh:mm:ss?
var d = new Date(); // for now
datetext = d.getHours()+":"+d.getMinutes()+":"+d.getSeconds();
I get this result below sometimes,
有时我会在下面得到这个结果,
12:10:1
which should be
这应该是
12:10:01
I assume this happens to the hour and minute as well.
我假设这也会发生在小时和分钟上。
So I am after this
所以我在这之后
01:01:01
Not this 1:1:1
不是这个 1:1:1
回答by Sunny R Gupta
Solution - (tl;dr version)
解决方案 - (tl;dr version)
datetext = d.toTimeString().split(' ')[0]
datetext = d.toTimeString().split(' ')[0]
Explanation:
解释:
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. :)
toTimeString
返回完整时间。我们按空间分割它以仅获取时间分量,然后取第一个有用的值。:)
Complete Flow:
完整流程:
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.
注意:这不需要您包含任何外部文件或库,因此执行所需的时间会更快。
回答by Michael
You can use this code snippet. I also added it to jsfiddle
and added type-safer comments, so make sure to check out the fiddle.
您可以使用此代码片段。我还将它添加到jsfiddle
并添加了类型更安全的注释,因此请务必查看小提琴。
If you're using jQuery, call it within your ready function. Otherwise you could use plain JavaScript to update the value of your field.
如果您使用的是 jQuery,请在您的就绪函数中调用它。否则,您可以使用纯 JavaScript 来更新字段的值。
$(document).ready(function(){
var d = new Date();
var formatted_time = time_format(d);
$('#yourTimeField').text(formatted_time);
});
Add these two methods to your script (you could also paste it in a single file, like it is in the jsfiddlecode snippet):
将这两个方法添加到您的脚本中(您也可以将其粘贴到单个文件中,就像在jsfiddle代码片段中一样):
function time_format(d) {
hours = format_two_digits(d.getHours());
minutes = format_two_digits(d.getMinutes());
seconds = format_two_digits(d.getSeconds());
return hours + ":" + minutes + ":" + seconds;
}
function format_two_digits(n) {
return n < 10 ? '0' + n : n;
}
That's it :) Hope it helps :)
就是这样:)希望它有帮助:)
回答by Jay Mayu
I'd suggest you to checkout some date/time library. Moment.jsis one good example.
我建议您查看一些日期/时间库。Moment.js就是一个很好的例子。
You could do simply as below
你可以简单地做如下
var d = new Date();
var formatteddatestr = moment(d).format('hh:mm:ss a');
Here is an example from fiddle
回答by Santosh
You can also try using http://momentjs.com/. A javascript date library for parsing, validating, manipulating, and formatting dates.
您也可以尝试使用http://momentjs.com/。用于解析、验证、操作和格式化日期的 JavaScript 日期库。