Skip to content Skip to sidebar Skip to footer

How Do I Find The Difference Between Two Dates Using Javascript

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 code If the date is next month 27

Solution 1:

Your code

date1=27/5/2012

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

date1 = 0.0026838966202783303

date1 will be a number, and this number has no getDate method.

If you declared them as actual date objects instead

var date2 = newDate(2012, 3, 19);
var date1 = newDate(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;

Solution 2:

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

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

Solution 3:

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

Solution 4:

I think you can subtract it:

var date2 = newDate(2012, 3, 19); // 1st argument = year, 2nd = month - 1 (because getMonth() return 0-11 not 1-12), 3rd = datevar date1 = newDate(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);

Solution 5:

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

var d = newDate();
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();
functiondstrToUTC(ds) {
    var dsarr = ds.split("/");
     var mm = parseInt(dsarr[0],10);
     var dd = parseInt(dsarr[1],10);
     var yy = parseInt(dsarr[2],10);
     returnDate.UTC(yy,mm-1,dd,0,0,0); }
    functiondatediff(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>");

Post a Comment for "How Do I Find The Difference Between Two Dates Using Javascript"