Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Calculate the number of days between two dates

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 467
    Comment on it

    If you want to calculate the number of days between two dates, you need to make a function,pass the dates and use getTime() function.

    Below is the example code :

    function GetNoDays(date1, date2) {
        var daysIndates = date1.getTime() - date2.getTime();
        //store the getTime diff 
        return (daysindates / (24 * 60 * 60 * 1000));
        //Convert values to -/+ days and return value
    }
    
    var d = new Date(2012, 6, 11);
    var d1 = new Date();
    var day = d1.getDate();
    var month = d1.getMonth() + 1;
    var year = d1.getFullYear();
    var curntdate = day + "/" + month + "/" + year;
    var curDate = new Date(year, month, day);
    alert(GetNoDays(curDate, d));
    

    First convert both dates then to obtain the number of days for a given number of milliseconds, we would divide it by 86,400,000, the number of milliseconds in a day (1000 x 60 seconds x 60 minutes x 24 hours).

    You can also use moment.js. For this the code looks like this:

    var a = moment('1/1/2012', 'DD/MM/YYYY');
    var b = moment('1/1/2013', 'DD/MM/YYYY');
    var days = b.diff(a, 'days');
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: