15

So the question is described in title. I need to get current second of the day using JavaScript.

Alan Whitelaw
  • 16,171
  • 9
  • 34
  • 51
Kirill Titov
  • 2,060
  • 5
  • 21
  • 33
  • 1
    Do you mean the seconds part of the current time, e.g., if it's 5:38:26 pm you want 26? Or do you mean the number of seconds since midnight? Or...? – nnnnnn Jul 12 '12 at 07:39

2 Answers2

29

You add up the bits:

var dt = new Date();
var secs = dt.getSeconds() + (60 * dt.getMinutes()) + (60 * 60 * dt.getHours());

or if you prefer

var dt = new Date();
var secs = dt.getSeconds() + (60 * (dt.getMinutes() + (60 * dt.getHours())));
bstoney
  • 6,594
  • 5
  • 44
  • 51
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
2

For example (in action):

​var today = new Date(), today_abs = new Date(), today_secs = 0;
today_abs.setHours(0);
today_abs.setMinutes(0);
today_abs.setSeconds(0);
today_secs = (today.getTime() - today_abs.getTime()) / 1000;​
fedosov
  • 1,989
  • 15
  • 26