How to format $.now() with Jquery
Solution 1
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;
}
Solution 2
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();
Solution 3
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
.
Solution 4
JSFiddle example here
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?
Solution 5
I'd suggest date-format jQuery plugin. Like this one or this one (I am using the former)
![Barış Velioğlu](https://i.stack.imgur.com/l4Cx3.jpg?s=256&g=1)
Barış Velioğlu
Updated on July 15, 2022Comments
-
Barış Velioğlu almost 2 years
$.now()
gives me the time as miliseconds. I need to show it something likehh:mm:ss
How can I do that in Jquery?
-
Mike Samuel almost 13 yearsNice, but if the time is 1AM you will get
1:0:0
instead of1:00:00
. -
kobe almost 13 years@vinod +1 nice and simple answer.
-
kobe almost 13 years+1 , for covering all use cases and nice usage of conditional operators
-
T.J. Crowder almost 13 years@Barış V.: Note that this has nothing to do with jQuery. (Nor does it need to.)
-
JB Nizet almost 13 yearsWhat about daylight saving time? Considering that all days have 24 hours is wrong, and this function will lead to incorrect times.
-
Gabriel Guimarães almost 13 yearsthis is pretty complex for something so simple don't you think? I just wish there was an easier way.
-
Mike Samuel almost 13 years@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 almost 13 yearsJB 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. -
sanmai almost 13 yearsWith this getting exact difference in time is as simple as ever:
(new Date(new Date(string) - new Date())).toUTCString().split(' ')[4]
-
GôTô almost 11 yearsDatepicker does not handle time!