How do I get the current date or/and time in seconds using Javascript?
How do I get the current date or/and time in seconds using Javascript?
Date.now()-Math.floor(Date.now()/1000/60/60/24)*24*60*60*1000
This should give you the milliseconds from the beginning of the day.
(Date.now()-Math.floor(Date.now()/1000/60/60/24)*24*60*60*1000)/1000
This should give you seconds.
(Date.now()-(Date.now()/1000/60/60/24|0)*24*60*60*1000)/1000
Same as previous except uses a bitwise operator to floor the amount of days.
On some day in 2020, inside Chrome 80.0.3987.132, this gives 1584533105
~~(new Date()/1000) // 1584533105
Number.isInteger(~~(new Date()/1000)) // true
There is no need to initialize a variable to contain the Date object due to the fact the Date.now() is a static method which means that is accessible directly from an API object's constructor.
document.write(Date.now()) // milliseconds
document.write(Date.now()/1000) // seconds
Live update of seconds since January 1, 1970 00:00:00 UTC
let element = document.getElementById('root')
const interval = setInterval(() => {
let seconds = Math.round(Date.now()/1000)
element.innerHTML = seconds
},1000)
Seconds since January 1, 1970 00:00:00 UTC
<h1 id='root'></h1>
To get today's total seconds of the day:
getTodaysTotalSeconds(){
let date = new Date();
return +(date.getHours() * 60 * 60) + (date.getMinutes() * 60) + date.getSeconds();
}
I have add + in return which return in int. This may help to other developers. :)
if you simply need seconds in THREE JS, use one of the code bellow in function uses window.requestAnimationFrame()
let sec = parseInt(Date.now().toString()[10]); console.log(' counting Seconds => '+ sec );
or
let currentTime= Date.now();
let secAsString= time.toString()[10];
let sec = parseInt(t);
console.log('counting Seconds =>'+ sec );