4

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?

Community
  • 1
  • 1
jQuerybeast
  • 13,589
  • 36
  • 117
  • 190
  • 1
    Possible duplicate of [How do you get a timestamp in JavaScript?](http://stackoverflow.com/questions/221294/how-do-you-get-a-timestamp-in-javascript) – GottZ Apr 25 '16 at 15:38

4 Answers4

4

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).

copy
  • 3,221
  • 2
  • 28
  • 35
2

Call Math.round.

SLaks
  • 837,282
  • 173
  • 1,862
  • 1,933
2

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
abuduba
  • 4,856
  • 7
  • 25
  • 42
1

Round it.

console.log(new Date().getTime() / 1000);
// 1326051145.787
console.log(Math.round(new Date().getTime() / 1000));
// 1326051146

Basic maths!

Lightness Races in Orbit
  • 369,052
  • 73
  • 620
  • 1,021