javascript 如何计算从今天到给定日期和 getTime() 代码之间的天数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12986068/
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-26 17:34:01  来源:igfitidea点击:

how to calculate number of days between today and given date and code for getTime()?

javascriptextjs

提问by Gihan Lasita

I want to calculate number of days between today and a given date and check whether how many days remaining until today or how many days past from today.

我想计算今天和给定日期之间的天数,并检查到今天还有多少天或从今天过去了多少天。

var today = new Date();
var date_to_reply = new Date('2012-10-15');
var timeinmilisec = today.getTime() - date_to_reply.getTime();
console.log( Math.floor(timeinmilisec / (1000 * 60 * 60 * 24)) );

this gives me 5as answer but how should i get (-5)since the date_to_reply is 5days past from today?

这给了我5作为答案但是我应该如何得到(-5)因为 date_to_reply 从今天起 5 天过去了?

is this the correct way to calculate any given date?

这是计算任何给定日期的正确方法吗?

Regards

问候

采纳答案by KooiInc

If you want the value to be negative (indicating date_to_replyis in the past) you should subtract the past date from the current: date_to_reply.getTime() - today.getTime().

如果您希望该值为负数(表示date_to_reply过去),您应该从当前日期中减去过去日期:date_to_reply.getTime() - today.getTime()

Check this linkfor ways to calculate more diffentiated results.

检查此链接以了解计算更多差异结果的方法。

回答by Fabian Lauer

What you are doing is correct: You want to calculate the difference(as number of days) between two dates. A difference can't be smaller than zero.

你在做什么是正确的:你想计算两个日期之间的差异(如天数)。差异不能小于零。

Although your date_to_replyis already in the past, theres still a 5 day difference.

虽然你date_to_reply已经过去了,但还有5天的差异。

So, everythings fine - it's the correct way.

所以,一切都很好 - 这是正确的方法。

EDIT: If you want a negative value as result, try this:

编辑:如果你想要一个负值作为结果,试试这个:

var today = new Date();
var date_to_reply = new Date('2012-10-15');
var timeinmilisec = date_to_reply.getTime() - today.getTime();
console.log( Math.ceil(timeinmilisec / (1000 * 60 * 60 * 24)) );

Remember you need to Math.ceilthe final result instead of rounding it down with Math.floor().

请记住,您需要Math.ceil得到最终结果,而不是将其四舍五入Math.floor()

回答by jahroy

If you swap the order of the dates, you'll get the negative number you want.

如果你交换日期的顺序,你会得到你想要的负数。

Better yet you could write a function that does this.

更好的是,您可以编写一个执行此操作的函数。

It could subtract the first parameter from the second.

它可以从第二个参数中减去第一个参数。

The second parameter could default to today.

第二个参数可以默认为今天。

function diffDates(dateOne, dateTwo) {
    if (typeof dateTwo === 'undefined') {
        dateTwo = new Date();
    }
    return dateOne.getTime() - dateTwo.getTime();
}

It would be better to have the function operate on numbers rather than dates.

让函数对数字而不是日期进行操作会更好。

That would be more flexible, but I'm typing on an iPad right now!

那会更灵活,但我现在正在 iPad 上打字!

回答by polin

Its obvious because today's date is greater than the previous. So either you need to make it negative on your own or use this

这很明显,因为今天的日期大于前一个。因此,要么您需要自己将其设为负数,要么使用它

var timeinmilisec = date_to_reply.getTime()-today.getTime();