我可能只是累了,没有清楚地思考,但是有人可以简洁地获得自使用JavaScript以来通过的毫秒数?
类似Date.getSeconds()
的东西,但这会返回毫秒。
尽管我只能做(Date.getSeconds()*1000) + Date.getMilliseconds()
,但这似乎真的很尴尬,就像有更好的方法一样。
谢谢!
取决于您要做什么。
现在至1分钟前毫秒至1分钟之间的区别应始终为60000毫秒。O_O
正如Jan所说,Date.now()
将返回当前的时间戳以毫秒为单位。
,但似乎您可能正在寻找GetTime方法,例如:https://developer.mozilla.org/en-us/docs/javascript/Reference/global_objects/date/getTime
// note the keyword "new" below
var date_instance = new Date();
// separate example in case you're managing Date()'s and not the method,
// who knows, just an example
var timestamp = date_instance.getTime();
var minute_before_timestamp = function(ts){
return ts - 60000;
};
console.log(minute_before_timestamp(timestamp));
console.log(minute_before_timestamp(date_instance.getTime()); // always same as above!
// or use the current time
console.log(minute_before_timestamp(Date.now()));
console.log(minute_before_timestamp(new Date().getTime()));
(另一个有用的链接:http://www.epochconverter.com/)
什么……
Date.now() % 60000
Date.now
返回MS中的当前UNIX时间戳。
要澄清那里发生的事情,%
运算符称为 modulo ,它的作用是,它为您提供了剩余的第一个数字。
一个示例可以是:
20 % 7 === 6
13 % 7 === 6
6 % 7 === 6
…因为…
20 / 7 = 2 + 6 / 7
13 / 7 = 1 + 6 / 7
6 / 7 = 0 + 6 / 7
(注意剩余的6
)