0

I have two arbitrary dates, lets say April 1st 2012 and January 15th 2013. I want to calculate the number of Sundays, Mondays, Tuesdays, Wednesdays, Thursdays, Fridays, and Saturdays between those two dates.

Is there a surefire-quick way to do this without crippling the users CPU/browser?

Thanks

Update

The premise of this is, we have a defined average number of events for any given day of the week. We need to calculate the number of events to happen in a time period, even for partials (like 1/2 day of Sunday would be half the number of events added to total)

Barry Chapman
  • 6,690
  • 3
  • 36
  • 64
  • Why did you explicitly list all weekdays? There are no days you do not want to count, do you? – Bergi May 14 '13 at 17:53
  • No I would like to count all days in the week :) I have found that on here, its best to be explicit in your needs. – Barry Chapman May 14 '13 at 17:54
  • Can I asume that you have the date in milliseconds? – Chango May 14 '13 at 17:55
  • looping over the number of days between then and now and incrementing a json object that contains an assortment of the days. – Barry Chapman May 14 '13 at 17:55
  • No, the date does not have to be in milliseconds, the nearest second is fine – Barry Chapman May 14 '13 at 17:56
  • Find how many full weeks are between your dates and then figure out the remaining days. That would be my approach – Shaded May 14 '13 at 17:57
  • @Shaded: And how would you calculate/count weeks? – Bergi May 14 '13 at 17:58
  • 1
    Aren't there 86400 seconds in a day? And aren't a lot of Unix timestamps in seconds since epoch? This suggests..... subtraction and division. – Paul May 14 '13 at 17:59
  • @Bergi, If I have the seconds between the timestamps I could divide by 607800 to figure out the full weeks, each one of those will increment each day by 1. Then I'm just left to figure out which days I have extras of. Which I haven't thought all the way through. – Shaded May 14 '13 at 18:01
  • Added an update. There are no other SO questions that relate to my question – Barry Chapman May 14 '13 at 18:04
  • @BarryChapman You may need to go read about the Poisson distribution. This is the usual way one formalizes constant probability of an event per unit time. http://en.wikipedia.org/wiki/Poisson_distribution – Paul May 14 '13 at 18:05
  • Also, I would note that some things, like web visits, are not uniform for all days of the week. – Paul May 14 '13 at 18:06

2 Answers2

2

Ok, here is a possible untested solution

date1 = new Date("2012-02-10");
date2 = new Date("2012-03-10");

daysInBetween = (date2.getTime() - date1.getTime())/1000/3600/24;

dayOfTheWeek1 = date1.getDay();

weeks = parseInt(daysInBetween/7, 10);
extraDays = daysInBetween%7;

You have weeks + 1 days of dayOfWeek1 ... dayOfWeek1 + (6 - extraDays)

You have weeks + 1 + extraDays days of dayOfWeek1 + (6 - extraDays) ... dayOfWeek1 + 6

Please take into acount that if dayOfWeek1 === 6 then I am assuming that dayOfWeek1 + 1 === 0.

EDIT:

A little bit more of code:

var days = {};
var dayOfTheWeekEnd = dayOfTheWeek1 + 6 - extraDays; // no imagination for names...
if (dayOfTheWeekEnd < 6) {
  if (0 >= dayOfTheWeek1 && 0 <= dayOfTheWeekEnd) {
    days.sunday = weeks + 1;
  } else {
    days.sunday = weeks + 1 + extraDays;
  }
  // etc for the other days, a for loog with an i instead of the 0 would be better.
} else {
  // I have to go the school! I'll edit it later.
  // The idea is that you have to take dayOfTheWeekEnd back to the 0-6 range
  // and check if its after dayOfWeek1 or before dayOfTheWeekEnd, then days.sunday=weeks+1.
}
Chango
  • 6,754
  • 1
  • 28
  • 37
  • Thanks @Scott! The answer has been changed. – Chango May 14 '13 at 18:20
  • 1
    [This answer](http://stackoverflow.com/a/544429/260389) has a great solution to OP's problem. – Chango May 14 '13 at 18:25
  • this is getting me very close - do i need to add a separate line item for each dayOfWeek2,3,4,5,6 etc? – Barry Chapman May 14 '13 at 18:38
  • I would recomend you use the answer I marked in the comment above, but yes, you could make a method that returns an object with the days for `0` (Sunday I think...), `1`, etc. – Chango May 14 '13 at 18:59
0

Thought it through... haven't tested it but I hope it helps.

function(date1, date2) {
   // date1 is assumed to be 12:00:00 am and date2 is actually the date after target at 
   // 12:00:00 am to make the dates inclusive so from the example date1 = 2012/4/1 and
   // date2 = 2013/1/16
   var timeBetween = date2.getTime() - date1.getTime(); //milliseconds between
   var weeks = timeBetween/1000/60/60/24/7;
   var sun = weeks;
   var mon = weeks;
   var tue = weeks;
   var wed = weeks;
   var thu = weeks;
   var fri = weeks;
   var sat = weeks;

   date1.setTime(date1.getTime() + weeks*7*24*60*60*1000); //set the first date to x weeks later
   //if this iterates more than 7 times then something is wrong.
   while(date1.getTime() < date2.getTime()){
       switch(date1.getDay()){
           case 0:
                sun++;
                break;
           case 1:
                mon++;
                break;
           ...
        }
        date1.setTime(date1.getTime() + 1000 * 60 * 60 * 24); //Move date 1 to the next date.
    }
}
Shaded
  • 17,276
  • 8
  • 37
  • 62