On Unix, I can run date \'+%s\'
to get the amount of seconds since epoch. But I need to query that in a browser front-end, not back-end.
Is there a way
My preferred way:
var msEpoch = (+new Date());
var sEpoch = (+new Date()) / 1000;
For more information on the +
jump down the rabbit hole.
EPOCH means time from 01 January 1970
var date = new Date();
Following line will return the number of milliseconds from 01 Jaunary 1970
var ms = date.getTime();
Following line will convert milliseconds to seconds
var seconds = Math.floor(ms/1000);
console.log("Seconds since epoch =",seconds);
You wanted seconds since epoch
function seconds_since_epoch(){ return Math.floor( Date.now() / 1000 ) }
example use
foo = seconds_since_epoch();
Try this:
new Date().getTime() / 1000
You might want to use Math.floor()
or Math.round()
to cut milliseconds fraction.