In JavaScript how to get second of current day? [closed]
-
20-06-2021 - |
Question
So the question is described in title. I need to get current second of the day using JavaScript.
Solution
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())));
OTHER TIPS
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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow