gpt4 book ai didi

javascript - 使用时刻的小时数之间的差异

转载 作者:行者123 更新时间:2023-12-02 16:03:45 24 4
gpt4 key购买 nike

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

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

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

Run - 从时间戳开始;​​

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

请记住时间戳如下:

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

我有这样的想法:

const time = moment(timestamp).diff(moment(), "minutes")

但老实说,我不知道如何改进它。你能帮我吗? 😔

最佳答案

不需要片刻

这应该可以帮助您入门

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)

关于javascript - 使用时刻的小时数之间的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69986530/

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