How to format $.now() with Jquery

Barış Velioğlu picture Barış Velioğlu · Jul 9, 2011 · Viewed 55.5k times · Source

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

How can I do that in Jquery?

Answer

vinod picture vinod · Jul 9, 2011

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();