有没有办法在不获取毫秒时间的情况下使用 Date().getTime() 函数?如果没有,是否有 .getTime() 的替代品,它只会给我以分钟为单位的精度?
我也不确定如何从日期对象中去除毫秒数。
var time = new Date().getTime()
Output: 1426515375925
最佳答案
简单的算术。如果您想要以秒为单位的值,请将毫秒结果除以 1000:
var seconds = new Date().getTime() / 1000;
不过,您可能想对其调用 Math.floor()
以删除所有小数:
var seconds = Math.floor(new Date().getTime() / 1000);
Is there a different function I can use that will give me only the minutes since 1/1/1970, I just dont want the number to be as precise.
当然,将秒除以 60,或者将毫秒除以 60000:
var minutes = Math.floor(new Date().getTime() / 60000);
var milliseconds = 1426515375925,
seconds = Math.floor(milliseconds / 1000), // 1426515375
minutes = Math.floor(milliseconds / 60000); // 23775256
关于javascript - .getTime() 替代没有毫秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29079575/