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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 22:34:46  来源:igfitidea点击:

How to format $.now() with Jquery

javascriptjquerydate-format

提问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 toStringmethod is basically an alias for toLocaleStringin 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 getMillisecondsyou should be OK) or toUTCString.

toString方法基本上是toLocaleString大多数实现中的别名。这将返回用户时区中的时间,而不是假设 UTC 使用毫秒getTime(如果使用的getMilliseconds话)或toUTCString.

回答by kobe

JSFiddle example here

JSFiddle示例在这里

http://jsfiddle.net/NHhMv/

http://jsfiddle.net/NHhMv/

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?

在哪里可以找到有关在 JavaScript 中格式化日期的文档?

回答by Bozho

I'd suggest date-format jQuery plugin. Like this oneor this one(I am using the former)

我建议使用日期格式的 jQuery 插件。像这个这个(我用的是前者)

回答by T.J. Crowder

jQuery doesn't have date formatting. You can roll your own with the JavaScript Dateobject, 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. momentjsis 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;
};