我如何使用 Javascript 找到两个日期之间的差异

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

How do i find the difference between two dates using Javascript

javascript

提问by Suresh Pattu

I want to get reaming days to go particular date so i am trying to detection of particular date with today date. but this not working here is my codeIf the date is next month 27 how can i get remaining days to go

我想获得特定日期的铰孔天数,所以我试图用今天的日期检测特定日期。但这在这里不起作用是我的代码如果日期是下个月 27 我怎么才能得到剩余的天数

    var date2=new Date();
    var date1=27/5/2012;
    var diff = date1.getDate()-date2.getDate();
    var date_reaming = diff.getDate();
    document.write(date_reaming + 'days to go');

采纳答案by Suresh Pattu

Here is the answer, i found this from heremy js-fiddle is here

这是答案,我从这里找到我的 js-fiddle 在这里

var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth();/* Returns the month (from 0-11) */
var curr_month_plus= curr_month+1; /* because if the month is  4 it will show output 3 so we have to add +1 with month*/ 
var curr_year = d.getFullYear();
function dstrToUTC(ds) {
    var dsarr = ds.split("/");
     var mm = parseInt(dsarr[0],10);
     var dd = parseInt(dsarr[1],10);
     var yy = parseInt(dsarr[2],10);
     return Date.UTC(yy,mm-1,dd,0,0,0); }
    function datediff(ds1,ds2) {
     var d1 = dstrToUTC(ds1);
     var d2 = dstrToUTC(ds2);
     var oneday = 86400000;
     return (d2-d1) / oneday;    }
    var a =curr_month_plus+ '/' + curr_date + '/' + curr_year;
    var b;   
    b = "5/26/2012";
    document.write(+datediff(a,b)+" day(s)<br>");

回答by dxh

Your code

你的代码

date1=27/5/2012

Actually means 27 divided by 5 divided by 2012. It is equivalent to writing

其实就是27除以5除以2012,相当于写

date1 = 0.0026838966202783303

date1 will be a number, and this number has no getDatemethod.

date1 将是一个数字,这个数字没有getDate方法。

If you declared them as actual date objects instead

如果您将它们声明为实际日期对象

var date2 = new Date(2012, 3, 19);
var date1 = new Date(2012, 4, 27);

You would be able to perform

你将能够执行

var diff = date1 - date2;

This would give you the difference in milliseconds between the two dates.

这将为您提供两个日期之间的毫秒差异。

From here, you could calculate the number of days like so:

从这里,您可以像这样计算天数:

var days = diff / 1000 / 60 / 60 / 24;

回答by thecodeparadox

function getDateDiff(date1, date2, interval) {
    var second = 1000,
    minute = second * 60,
    hour = minute * 60,
    day = hour * 24,
    week = day * 7;
    date1 = new Date(date1).getTime();
    date2 = (date2 == 'now') ? new Date().getTime() : new Date(date2).getTime();
    var timediff = date2 - date1;
    if (isNaN(timediff)) return NaN;
    switch (interval) {
    case "years":
        return date2.getFullYear() - date1.getFullYear();
    case "months":
        return ((date2.getFullYear() * 12 + date2.getMonth()) - (date1.getFullYear() * 12 + date1.getMonth()));
    case "weeks":
        return Math.floor(timediff / week);
    case "days":
        return Math.floor(timediff / day);
    case "hours":
        return Math.floor(timediff / hour);
    case "minutes":
        return Math.floor(timediff / minute);
    case "seconds":
        return Math.floor(timediff / second);
    default:
        return undefined;
    }
}

console.log(getDateDiff('19/04/2012', '27/5/2012', 'days'));

回答by Juan G. Hurtado

jQuery has not built-in date management functions. Try with: http://momentjs.com/

jQuery 没有内置日期管理功能。尝试使用:http: //momentjs.com/

回答by Muhammad Alvin

I think you can subtract it:

我认为你可以减去它:

var date2 = new Date(2012, 3, 19); // 1st argument = year, 2nd = month - 1 (because getMonth() return 0-11 not 1-12), 3rd = date
var date1 = new Date(2012, 4, 27);
var distance = date1.getTime() - date2.getTime();
distance = Math.ceil(distance / 1000 / 60 / 60 / 24); // convert milliseconds to days. ceil to round up.
document.write(distance);

回答by Ropstah

Please understand what jQuery is made for.

请了解 jQuery 的用途。

jQuery is a fast and concise JavaScript Library that simplifies HTML document traversing, event handling, animating, and Ajax interactionsfor rapid web development.

jQuery 是一个快速而简洁的 JavaScript 库,它简化了HTML 文档遍历事件处理动画Ajax 交互,以实现快速的 Web 开发。

You want to use basicJavacript or as Juan G. Hurtado states another library like momentjs.

你想使用基本的Javacript 或者像 Juan G. Hurtado 所说的另一个库,比如momentjs