Get current date/time in seconds
var seconds = new Date().getTime() / 1000;
....will give you the seconds since midnight, 1 Jan 1970
Reference
Date.now()
gives milliseconds since epoch. No need to use new
.
Check out the reference here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now
(Not supported in IE8.)
Using new Date().getTime() / 1000
is an incomplete solution for obtaining the seconds, because it produces timestamps with floating-point units.
const timestamp = new Date() / 1000; // 1405792936.933
// Technically, .933 would be milliseconds.
A better solution would be:
// Rounds the value
const timestamp = Math.round(new Date() / 1000); // 1405792937
// - OR -
// Floors the value
const timestamp = new Date() / 1000 | 0; // 1405792936
Values without floats are also safer for conditional statements, as the float may produce unwanted results. The granularity you obtain with a float may be more than needed.
if (1405792936.993 < 1405792937) // true