使用时刻的小时之间的差异

Fra*_*des 2 javascript node.js momentjs

我知道这是互联网上的一个热门问题。但关键是我需要准确地做一些事情,并且由于我不太熟悉日期处理,所以我需要问这个问题。

我的问题如下,从时间戳我必须定义某些操作。这些行动是在以下条件下采取的:

Prep- 在时间戳之前大约一小时开始;

Run- 从时间戳开始;

Reset- 如果时间戳过去 15 分钟,则执行此操作;

请记住,时间戳如下:

const timestamp = "2021-11-16T09:18:02+0000"
Run Code Online (Sandbox Code Playgroud)

我有这样的想法:

const time = moment(timestamp).diff(moment(), "minutes")
Run Code Online (Sandbox Code Playgroud)

但说实话,我不知道如何改进这一点。你能帮我吗?

mpl*_*jan 5

无需片刻

这应该可以帮助你开始

const timestamp = "2021-11-16T09:18:02+0000"

const d = new Date(timestamp)

const prep = new Date(timestamp);
prep.setHours(prep.getHours()-1);// Prep - starts about an hour before timestamp;

// Run - starts right at timestamp;


const resetTime = new Date(timestamp)
// Reset - this action is taken if 15 minutes have passed after the timestamp;
resetTime.setMinutes(resetTime.getMinutes()-15)

console.log(d)
console.log(prep)
console.log(resetTime)

console.log((d.getTime()-prep.getTime())/60000)
Run Code Online (Sandbox Code Playgroud)