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
  • 14,130
  • 38
  • 118
  • 196
  • 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,301
  • 2
  • 29
  • 36
2

Call Math.round.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
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,986
  • 7
  • 26
  • 43
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
  • 378,754
  • 76
  • 643
  • 1,055