So the question is described in title. I need to get current second of the day using JavaScript.
Asked
Active
Viewed 1.5k times
15
-
1Do 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 Answers
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
-
2If you plan to store it somewhere consider using `getUTCHours` and `getUTCMinutes` – Murali KG Jan 20 '17 at 07:26