javascript 日期差异

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

javascript date difference

javascriptdate

提问by williamparry

I've looked at: Get difference between 2 dates in javascript?

我看过:在 javascript 中获取两个日期之间的差异?

And I still can't get this to work.

我仍然无法让它发挥作用。

var difference = data.List[0].EndDate - Math.round(new Date().getTime()/1000.0) * 1000;
var daysRemaining = Math.floor(difference / 1000 / 60 / 60 / 24);
var hoursRemaining = Math.floor(difference / 1000 / 60 / 60 - (24 * daysRemaining));
var minutesRemaining = Math.floor(difference / 1000 / 60 - (24 * 60 * daysRemaining) - (60 * hoursRemaining));
var secondsRemaining = Math.floor(difference / 1000 - (24 * 60 * 60 * daysRemaining) - (60 * 60 * hoursRemaining) - (60 * minutesRemaining));

data.List[0].EndDate is a UTC number (like: 1291427809310 (http://www.epochconverter.com/)) that will always be later than the current date.

data.List[0].EndDate 是一个 UTC 数字(如:1291427809310(http://www.epochconverter.com/)),它总是晚于当前日期。

回答by Devil's Advocate

function days_between(date1, date2) {

    // The number of milliseconds in one day
    var ONE_DAY = 1000 * 60 * 60 * 24

    // Convert both dates to milliseconds
    var date1_ms = date1.getTime()
    var date2_ms = date2.getTime()

    // Calculate the difference in milliseconds
    var difference_ms = Math.abs(date1_ms - date2_ms)

    // Convert back to days and return
    return Math.round(difference_ms/ONE_DAY)

}

http://www.mcfedries.com/javascript/daysbetween.asp

http://www.mcfedries.com/javascript/daysbetween.asp

回答by Devil's Advocate

I now believe thisis the best solution:

我现在相信是最好的解决方案:

http://momentjs.com/

http://momentjs.com/

回答by epascarello

You say that UTC timestamp is "2004-09-16T23:59:58.75"?

你说UTC时间戳是“2004-09-16T23:59:58.75”?

So you are basically doing

所以你基本上是在做

var x = "2004-09-16T23:59:58.75" - 123456

Now that you clarified that, than the above does not apply. You new issue is the number of milliseconds is in the past so when you do the difference calculation, you are getting a negative number. You probably want to swap the order around.

既然您澄清了这一点,以上内容并不适用。您的新问题是过去的毫秒数,因此当您进行差异计算时,您会得到一个负数。您可能想要交换顺序。

var difference = new Date().getTime()-data.List[0].EndDate;

回答by Dan Grossman

If EndDate is in milliseconds, and getTime() returns milliseconds, why do you divide it by 1000 only to multiply it by 1000 in the same line? And if you only need second precision for all the rest of the code, why work in milliseconds? Start out with a number of seconds to simplify all your calculations:

如果 EndDate 以毫秒为单位,并且 getTime() 返回毫秒,为什么在同一行中将其除以 1000 却只乘以 1000?如果您只需要所有其余代码的第二个精度,为什么要以毫秒为单位?从几秒钟开始以简化您的所有计算:

var difference = Math.round((data.List[0].EndDate - new Date().getTime()) / 1000);