1

I am writing an application that contains a datepicker. When you click on a date in the datepicker, the date shows up in an input-field (type="text"). For example, if you choose tomorrow's day it shows:

09.02.2014

So as you can guess the format is dd/mm/yyyy.

For the application I need to calculate the number of days between the date picked and the current date. I already know how to generate the current date with jQuery. But I have no clue how to calculate the time difference in days. Anyone maybe can help out with a code?

Kent Miller
  • 499
  • 2
  • 8
  • 21

2 Answers2

1

Subtracting two date will return you span in millisecond then convert it into days:

function daydiff(first, second) {
    return (second-first)/(1000*60*60*24)
}
alert(daydiff(date1, date2));
Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110
1

H i , A traditional method would look something like this.

Starting with the date value in the dot notation format we can convert to the dd/mm/yyyy format.

UPDATE as comment, given that the date string needs reordering into MM/DD/YYYY

 var inputval = "09.02.2014".split(".");
 var dateval = new Date(inputval[1]+"/"+inputval[0]+"/"+inputval[2]);


 /* with our Date object we can now compare */
 var currDate = new Date();
 var diffDays = Math.round(Math.abs((dateval.getTime() - currDate.getTime())/(24*60*60*1000)));

The var diffdays = quick date compare snippet taken from :

How to calculate the number of days between two dates using JavaScript?

Worth to mention about adding all this into a function for re-usability.

function daysDifferenceFromToday(datestr) {
 var inputval = datestr.split(".");
 var dateval = new Date(inputval[1]+"/"+inputval[0]+"/"+inputval[2]);
 var currDate = new Date();
 return Math.round(Math.abs((dateval.getTime() - currDate.getTime())/(24*60*60*1000)));

}

/* usages */
alert(daysDifferenceFromToday("09.02.2014"));

if(daysDifferenceFromToday("05.02.2014")>2) {
  ... 
 }
Community
  • 1
  • 1
Rob Sedgwick
  • 5,216
  • 4
  • 20
  • 36
  • thank you, but your operation in dateval provides the wrong date: it provides 2nd of September... but instead it needs to provide 9th of February... how can I solve this? – Kent Miller Feb 08 '14 at 13:32
  • Oh good spot, will update the answer ... – Rob Sedgwick Feb 08 '14 at 13:42
  • just made an update again, as I had incorrectly copied the 'oneday' variable from the reference link , updated now to also show this in a reusable utility – Rob Sedgwick Feb 08 '14 at 13:55
  • unfortunately, your code still doesn't work. using your code, I get the wrong results: if the current date equals the target date I get "1" as a day difference; if the target date is tomorrow, I get "0" and if the target date is the day after tomorrow I get "1"... how can this happen? – Kent Miller Feb 08 '14 at 14:32
  • Hi Kent, you could follow the link in the answer relating to the date compare part - http://stackoverflow.com/questions/2627473/how-to-calculate-the-number-of-days-between-two-dates-using-javascript - here is another - http://stackoverflow.com/questions/542938/how-do-i-get-the-number-of-days-between-two-dates-in-javascript – Rob Sedgwick Feb 08 '14 at 14:40
  • .. that snippet was taken from the an answer on SO and linked in , as the link contains more information to that part of the solution – Rob Sedgwick Feb 08 '14 at 14:47