According to this: Get current date/time in seconds
var seconds = new Date().getTime() / 1000;
gives you the time in seconds. But the time given is a decimal number. How can I turn it into whole number?
According to this: Get current date/time in seconds
var seconds = new Date().getTime() / 1000;
gives you the time in seconds. But the time given is a decimal number. How can I turn it into whole number?
You do Math.round(new Date().getTime() / 1000)
or a short (and faster version):
new Date().getTime() / 1000 | 0
Using this binary operator will zero the floating number part of your number (and therefore round it down).
Fastest and simplest: Force date to be represented as number by made math operation directly on date object. Parentheses can be ommited where we calling to constructor without arguments
new Date/1000|0 // => 1326184656
+new Date == new Date().getTime() // true
Explanation:
new Date // => Tue Jan 10 2012 09:22:22 GMT+0100 (Central Europe Standard Time)
By apply +
operator
+new Date //=> 1326184009580
Round it.
console.log(new Date().getTime() / 1000);
// 1326051145.787
console.log(Math.round(new Date().getTime() / 1000));
// 1326051146
Basic maths!