Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format $.now() with Jquery

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

How can I do that in Jquery?

like image 575
Barış Velioğlu Avatar asked Jul 09 '11 21:07

Barış Velioğlu


People also ask

How to display current time in jQuery?

You can use Date() in JS.

How to change date format in jQuery?

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".

How can I get current date in jQuery in dd mm yyyy format?

You can do it like that: var d = new Date(); var month = d. getMonth()+1; var day = d. getDate(); var output = d.

How to show date and time in jQuery?

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


2 Answers

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();
like image 137
vinod Avatar answered Sep 24 '22 00:09

vinod


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;
}
like image 41
Mike Samuel Avatar answered Sep 23 '22 00:09

Mike Samuel