Javascript 如何使用 Jquery 格式化 $.now()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6637574/
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
How to format $.now() with Jquery
提问by Bar?? Velio?lu
$.now()
gives me the time as miliseconds. I need to show it something like hh:mm:ss
$.now()
给我时间以毫秒为单位。我需要向它展示类似的东西hh:mm:ss
How can I do that in Jquery?
我怎样才能在 Jquery 中做到这一点?
回答by vinod
I'd suggest just using the Javascript Date object for this purpose.
我建议为此目的仅使用 Javascript Date 对象。
var d = new Date();
var time = d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
Edit: I just came across the method below, which covers formatting issues such as the one mike-samuel mentioned and is cleaner:
编辑:我刚刚遇到了下面的方法,它涵盖了格式问题,例如提到的 Mike-samuel 并且更清晰:
var time = d.toLocaleTimeString();
回答by Mike Samuel
function formatTimeOfDay(millisSinceEpoch) {
var secondsSinceEpoch = (millisSinceEpoch / 1000) | 0;
var secondsInDay = ((secondsSinceEpoch % 86400) + 86400) % 86400;
var seconds = secondsInDay % 60;
var minutes = ((secondsInDay / 60) | 0) % 60;
var hours = (secondsInDay / 3600) | 0;
return hours + (minutes < 10 ? ":0" : ":")
+ minutes + (seconds < 10 ? ":0" : ":")
+ seconds;
}
回答by Cristian Sanchez
new Date().toString().split(' ')[4]
or
或者
new Date().toString().match(/\d{2}:\d{2}:\d{2}/)[0]
The toString
method is basically an alias for toLocaleString
in most implementations. This will return the time in the user's timezone as opposed to assuming UTC by using milliseconds if you use getTime
(if you use getMilliseconds
you should be OK) or toUTCString
.
该toString
方法基本上是toLocaleString
大多数实现中的别名。这将返回用户时区中的时间,而不是假设 UTC 使用毫秒getTime
(如果使用的getMilliseconds
话)或toUTCString
.
回答by kobe
JSFiddle example here
JSFiddle示例在这里
The jquery now is nothing but
jquery 现在不过是
The $.now() method is a shorthand for the number returned by the expression
$.now() 方法是表达式返回的数字的简写
(new Date).getTime().
from jquery
来自 jquery
http://api.jquery.com/jQuery.now/
http://api.jquery.com/jQuery.now/
and follow this link
并点击此链接
Where can I find documentation on formatting a date in JavaScript?
回答by Bozho
回答by T.J. Crowder
jQuery doesn't have date formatting. You can roll your own with the JavaScript Date
object, or you can use a library that does it for you. DateJSis one such library, which provides a rich set of formatting, parsing, and manipulation functionality. However, it hasn't been maintained in years. momentjs
is under active development.
jQuery 没有日期格式。您可以使用 JavaScriptDate
对象创建自己的对象,也可以使用为您执行此操作的库。DateJS就是这样一个库,它提供了一组丰富的格式化、解析和操作功能。然而,多年来一直没有得到维护。momentjs
正在积极开发中。
回答by Dylan Hayes
I'm way late to this, but thought i'd just throw this little snippet out there for the masses. This is something I use just to get a quick localized timestamp. It's pretty clean and handy.
我迟到了,但我想我只是把这个小片段扔给大众。这是我用来快速获取本地化时间戳的东西。它非常干净和方便。
function getStamp() {
var d = new Date();
var mm = d.getMilliseconds(), hh = d.getHours(),
MM = d.getMinutes(), ss = d.getSeconds();
return (hh < 10 ? "0" : "") + hh + (MM < 10 ? ":0" : ":") + MM + (ss < 10 ? ":0" : ":") + ss + ":" + mm;
};