javascript getTime() to 10 digits only
Try dividing it by 1000, and use parseInt method.
const t = parseInt(Date.now()/1000);
console.log(t);
I think you just have to divide it by 1000 milliseconds and you'll get time in seconds
Math.floor(date.getTime()/1000)
If brevity is ok, then:
function secondsSinceEpoch() {
return new Date/1000 | 0;
}
Where:
new Date
is equivalent tonew Date()
| 0
truncates the decimal part of the result and is equivalent toMath.floor(new Date/1000)
(see What does |0 do in javascript).
Using newer features, and allowing for a Date to be passed to the function, the code can be reduced to:
let getSecondsSinceEpoch = (x = new Date) => x/1000 | 0;
But I prefer function declarations as I think they're clearer.