Skip to content
Advertisement

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, for example, 05:45

How can I assign this to a variable?

Advertisement

Answer

var d = new Date("2011-04-20T09: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
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement