gpt4 book ai didi

javascript - javascript 自上一分钟以来经过的毫秒数

转载 作者:行者123 更新时间:2023-12-02 12:39:11 25 4
gpt4 key购买 nike

我可能只是累了并且没有清晰地思考,但是有人可以提供一种简洁的方法来使用 javascript 获取自上一分钟以来经过的毫秒数吗?

类似Date.getSeconds(),但会返回毫秒。

虽然我可以这样做(Date.getSeconds()*1000) + Date.getMilliseconds(),但这看起来真的很尴尬,而且必须有更好的方法。

谢谢!

最佳答案

取决于你想做什么。

现在与 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/)

关于javascript - javascript 自上一分钟以来经过的毫秒数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13074829/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com