admin管理员组

文章数量:1279113

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?

Share Improve this question edited May 23, 2017 at 12:16 CommunityBot 11 silver badge asked Jan 8, 2012 at 19:29 jQuerybeastjQuerybeast 14.5k39 gold badges119 silver badges198 bronze badges 1
  • 1 Possible duplicate of How do you get a timestamp in JavaScript? – GottZ Commented Apr 25, 2016 at 15:38
Add a ment  | 

4 Answers 4

Reset to default 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).

Call Math.round.

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!

本文标签: