hss hss - 6 months ago 10
Javascript Question

How do I get the current time only in JavaScript

How can I get the current time in JavaScript and use it in a timepicker?

I tried

var x = date()
and got:


Tue May 15 2012 05:45:40 GMT-0500


But I need only current time, like 05:45

How can I assign this to a variable?

Answer
var d = new Date("2011-04-20 09:30:51.01");
d.getHours(); // => 9
d.getMinutes(); // =>  30
d.getSeconds(); // => 51

or

var d = new Date(); // for now
d.getHours(); // => 9
d.getMinutes(); // =>  30
d.getSeconds(); // => 51
Comments