$.now()
gives me the time as miliseconds. I need to show it something like hh:mm:ss
How can I do that in Jquery?
You can use Date() in JS.
So if we want to use jquery in our program, we should be use moment js plugin so that we can change the date format. Now we will provide a simple example in which we are giving three dates in different formats, and then we will convert it into the format "DD-MM-YYYY".
You can do it like that: var d = new Date(); var month = d. getMonth()+1; var day = d. getDate(); var output = d.
Please check out this code (jsFiddle). <span id="date"></span> var now = new Date(); dateFormat(now, "dddd, mmmm dS, yyyy, h:MM:ss TT"); // Saturday, June 9th, 2007, 5:46:21 PM $('#date'). append(now);
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();
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With