【发布时间】:2012-02-05 12:31:10
【问题描述】:
据此:Get current date/time in seconds
var seconds = new Date().getTime() / 1000; 为您提供以秒为单位的时间。但给出的时间是十进制数。我怎样才能把它变成整数?
【问题讨论】:
标签: javascript object date time
据此:Get current date/time in seconds
var seconds = new Date().getTime() / 1000; 为您提供以秒为单位的时间。但给出的时间是十进制数。我怎样才能把它变成整数?
【问题讨论】:
标签: javascript object date time
圆它。
console.log(new Date().getTime() / 1000);
// 1326051145.787
console.log(Math.round(new Date().getTime() / 1000));
// 1326051146
基础数学!
【讨论】:
最快最简单的: 通过直接对日期对象进行数学运算,强制将日期表示为数字。 在我们调用不带参数的构造函数时可以省略括号
new Date/1000|0 // => 1326184656
+new Date == new Date().getTime() // true
解释:
new Date // => Tue Jan 10 2012 09:22:22 GMT+0100 (Central Europe Standard Time)
通过申请+运营商
+new Date //=> 1326184009580
【讨论】:
您可以使用Math.round(new Date().getTime() / 1000) 或简短(更快的版本):
new Date().getTime() / 1000 | 0
使用此二元运算符会将数字的浮点数部分归零(因此向下舍入)。
【讨论】:
致电Math.round。
【讨论】: