Reg Exp
Web Design
Notes Client
Days Between Dates
For a recent project, I needed the ability to compute the number of days between two dates. In Notes formula language, this is pretty easy to do. But I needed it done on the web. After a little searching, I found a piece of code on http://javascript.internet.com that I was able to modify to fit my needs. I modified the code to be a generic function, and thought I'd share it.

function daysBetween(date1, date2){
   if (date1.indexOf("-") != -1) { date1 = date1.split("-"); } else if (date1.indexOf("/") != -1) { date1 = date1.split("/"); } else { return 0; }
   if (date2.indexOf("-") != -1) { date2 = date2.split("-"); } else if (date2.indexOf("/") != -1) { date2 = date2.split("/"); } else { return 0; }
   if (parseInt(date1[0], 10) >= 1000) {
       var sDate = new Date(date1[0]+"/"+date1[1]+"/"+date1[2]);
   } else if (parseInt(date1[2], 10) >= 1000) {
       var sDate = new Date(date1[2]+"/"+date1[0]+"/"+date1[1]);
   } else {
       return 0;
   if (parseInt(date2[0], 10) >= 1000) {
       var eDate = new Date(date2[0]+"/"+date2[1]+"/"+date2[2]);
   } else if (parseInt(date2[2], 10) >= 1000) {
       var eDate = new Date(date2[2]+"/"+date2[0]+"/"+date2[1]);
   } else {
       return 0;
   var one_day = 1000*60*60*24;
   var daysApart = Math.abs(Math.ceil((sDate.getTime()-eDate.getTime())/one_day));
   return daysApart;

Note that it is assumed that string dates are passed in with a date separator of either a dash or a slash. The Date function in JavaScript takes a year, then a month (0-11), then a day as parameters. The function looks for 4 digits in either the first "piece" or the last "piece" and sets that as the year. For the month and the day, the first "non four digit" appearance is the month and the second one is the day. So basically the format can be YYYY-MM-DD or MM-DD-YYYY and the separator can be either a slash or a dash and the month and day can be either one digit or two digits (the year must be four digits).

You can try out the function here:
Date 1:
Date 2:

Days Between: