How to get current time with jQuery
You may try like this:
new Date($.now());
Also using Javascript you can do like this:
var dt = new Date();
var time = dt.getHours() + ":" + dt.getMinutes() + ":" + dt.getSeconds();
document.write(time);
Convert a Date
object to an string, using one of Date.prototype
's conversion getters, for example:
var d = new Date();
d+''; // "Sun Dec 08 2013 18:55:38 GMT+0100"
d.toDateString(); // "Sun Dec 08 2013"
d.toISOString(); // "2013-12-08T17:55:38.130Z"
d.toLocaleDateString() // "8/12/2013" on my system
d.toLocaleString() // "8/12/2013 18.55.38" on my system
d.toUTCString() // "Sun, 08 Dec 2013 17:55:38 GMT"
Or, if you want it more customized, see the list of Date.prototype
's getter methods.
You need to fetch all "numbers" manually
like this:
var currentdate = new Date();
var datetime = "Now: " + currentdate.getDate() + "/"
+ (currentdate.getMonth()+1) + "/"
+ currentdate.getFullYear() + " @ "
+ currentdate.getHours() + ":"
+ currentdate.getMinutes() + ":"
+ currentdate.getSeconds();
document.write(datetime);
You don't need to use jQuery for this!
The native JavaScript implementation is Date.now()
.
Date.now()
and $.now()
return the same value:
Date.now(); // 1421715573651
$.now(); // 1421715573651
new Date(Date.now()) // Mon Jan 19 2015 20:02:55 GMT-0500 (Eastern Standard Time)
new Date($.now()); // Mon Jan 19 2015 20:02:55 GMT-0500 (Eastern Standard Time)
..and if you want the time formatted in hh-mm-ss:
var now = new Date(Date.now());
var formatted = now.getHours() + ":" + now.getMinutes() + ":" + now.getSeconds();
// 20:10:58