-1

I am storing the dates in these two variables. How to subtract these two dates to get number of days.Here in one variable the date has been got from the database and another variable holds the today date. Now I want to find the difference in days. some thing like var numberofdays= dt_ret-dt_due;

var dt_due= document.getElementById("duedate").value=a.four;
var dt_ret=today;
  • 4
    I googled the question, and found a similar question (http://stackoverflow.com/questions/3224834/get-difference-between-2-dates-in-javascript). Even that is marked as a duplicate (http://stackoverflow.com/questions/542938/how-do-i-get-the-number-of-days-between-two-dates-in-javascript) – Chris Lear Oct 28 '15 at 14:29

2 Answers2

0

Try this:

var numberofmilliseconds = new Date( dt_due ).getTime( ) - new Date( dt_ret ).getTime( ),
    numberofdays = numberofmilliseconds / 1000 / 60 / 60 / 24;

Did that do the trick?

Edoardo L'Astorina
  • 7,235
  • 2
  • 12
  • 9
0

The date comming from database is string i consider, all you have to do is to convert your string date to js Date object and take a diffrence :

Date date1 = new Date('2015-10-28'); // DB date
Date date2 = new Date(); // current date

var diff = new Date(date2.getTime() - date1.getTime());
console.log(diff.getUTCDate() - 1); // Gives day count of difference
Amey Jadiye
  • 3,066
  • 3
  • 25
  • 38