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?
- 1 Possible duplicate of How do you get a timestamp in JavaScript? – GottZ Commented Apr 25, 2016 at 15:38
4 Answers
Reset to default 4You 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!
本文标签:
版权声明:本文标题:javascript - How can I convert the (fractional) result of `new Date().getTime()1000` into a whole number? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741303346a2371223.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论