14

$.now() gives me the time as miliseconds. I need to show it something like hh:mm:ss

How can I do that in Jquery?

Nakilon
  • 34,866
  • 14
  • 107
  • 142
Barış Velioğlu
  • 5,709
  • 15
  • 59
  • 105

7 Answers7

39

I'd suggest just using the Javascript Date object for this purpose.

    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:

    var time = d.toLocaleTimeString();
vinod
  • 2,358
  • 19
  • 26
24
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;
}
Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
  • +1 , for covering all use cases and nice usage of conditional operators – kobe Jul 09 '11 at 21:41
  • @Barış V.: Note that this has nothing to do with jQuery. (Nor does it need to.) – T.J. Crowder Jul 09 '11 at 21:52
  • What about daylight saving time? Considering that all days have 24 hours is wrong, and this function will lead to incorrect times. – JB Nizet Jul 09 '11 at 22:36
  • this is pretty complex for something so simple don't you think? I just wish there was an easier way. – Gabriel Guimarães Jul 09 '11 at 22:42
  • 1
    @Gabriel, you can combine my approach to get the formatting right with Vinod's use of the get methods thus: `var hours = d.getHours(), minutes = d.getMinutes(), seconds = d.getSeconds(); return hours + (minutes < 10 ? ":0" : ":") + minutes ...` – Mike Samuel Jul 09 '11 at 22:51
  • JB Nizet, that is correct. If you know the timezone offset, you can just add timezoneOffsetInMinutes * 60 to secondsSinceEpoch. If you want to use the local timezone, that offset is available obliquely via the `Date` API. Otherwise, you really have to define a timezone which can be a hard task. RFC 5545 comes up with a definition based on recurrences which satisfies its need to allow reproducible timezone definitions. – Mike Samuel Jul 09 '11 at 22:54
4

JSFiddle example here

http://jsfiddle.net/NHhMv/

The jquery now is nothing but

The $.now() method is a shorthand for the number returned by the expression

(new Date).getTime().

from jquery

http://api.jquery.com/jQuery.now/

and follow this link

Where can I find documentation on formatting a date in JavaScript?

Community
  • 1
  • 1
kobe
  • 15,671
  • 15
  • 64
  • 91
4
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.

Cristian Sanchez
  • 31,171
  • 11
  • 57
  • 63
  • With this getting exact difference in time is as simple as ever: `(new Date(new Date(string) - new Date())).toUTCString().split(' ')[4]` – sanmai Jul 10 '11 at 03:00
2

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

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
1

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;
};
Dylan Hayes
  • 2,331
  • 1
  • 23
  • 33
1

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. DateJS is 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.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875