javascript 检查日期是否为 24 小时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51405133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 09:32:26  来源:igfitidea点击:

Check if a date is 24 hours old

javascriptdate

提问by ETX

I have a created date. I want to run an if/switch statement to know whether the date and time right now is more than 24 hours from the created date.

我有一个创建日期。我想运行 if/switch 语句以了解当前的日期和时间是否距创建日期超过 24 小时。

If the created date is more than 24 hours ago, do something, if it's less than 24 hours ago do something else.

如果创建日期早于 24 小时前,请执行某些操作,如果少于 24 小时前,则执行其他操作。

let now = +new Date();

// This is returned as: July 18, 2018 at 3:48:00 AM UTC+1
let createdAt = +doc.data().created_at;

const oneDay = 60 * 60 * 24;
var compareDatesBoolean = (now - createdAt) > oneDay;

Any guidance here would be great thank you! :)

这里的任何指导都会非常感谢!:)

采纳答案by sairohith

Add this const oneday = 60 * 60 * 24 * 1000 (milliseconds). Since, JS converts difference between 2 Datetime objects into milliseconds. I think that should work

添加这个 const oneday = 60 * 60 * 24 * 1000 (milliseconds)。因为,JS 将 2 个 Datetime 对象之间的差异转换为毫秒。我认为这应该有效

回答by Hitesh Anshani

Created Example For One hour Refer This and Then Change it to 24 Hours

一小时创建示例请参阅此内容,然后将其更改为 24 小时

const OneHourAgo= (date) => {
    const hour= 1000 * 60 * 60;
    const hourago= Date.now() - hour;

    return date > hourago;
}

Simple One:-

简单一:-

var OneDay = new Date().getTime() + (1 * 24 * 60 * 60 * 1000)
                                     day hour  min  sec  msec
if (OneDay > yourDate) {
    // The yourDate time is less than 1 days from now
}
else if (OneDay < yourDate) {
    // The yourDate time is more than 1 days from now
}

回答by ZER0

Maybe something like that?

也许是这样的?

const now = Date.now();

// if it's a firebase timestamp
const createdAt = doc.data().created_at.toMillis(); 

const oneDay = 24 * 60 * 60 * 1000;

const isMoreThanADay = (now - createdAt) > oneDay;

I don't know firebase timestampsI relied on the documentation for the toMillismethod.

我不知道我依赖于该方法的文档的firebase 时间戳toMillis

回答by Umang Loriya

Using moment will be much easier in this case, You could try this:

在这种情况下使用 moment 会容易得多,你可以试试这个:

    let hours = moment().diff(moment(yourDateString), 'hours');
    let days = moment().diff(moment(yourDateString), 'days');

It will give you integer value like 1,2,5,0etc so you can easily use condition check like:

它会给你像 1,2,5,0etc 这样的整数值,所以你可以很容易地使用条件检查,比如:

if(hours >= 24) { your code }

or

或者

if(days >= 1) { your code }

Also, one more thing is you can get more accurate result of the time difference (in decimals like 1.2,1.5,0.7etc) to get this kind of result we need to pass 3rd argument which i.e. true for precise result use this syntax:

此外,还有一件事是您可以获得更准确的时间差结果(以小数形式,如 1.2、1.5、0.7 等)来获得这种结果,我们需要传递第三个参数,即对于精确结果为真,请使用以下语法:

let hours = moment().diff(moment(yourDateString), 'hours', true);

Let me know if you have any further query

如果您有任何进一步的疑问,请告诉我