Count saturdays and sundays between two dates

G

gunso

Hi
How do i count number of saturdays and sundays in a function which
takes in 2 dates...

function countSatSun(startDate, endDate){

}

Should return me the total of saturdays and sundays between these
dates.

Thanks in advance
 
T

Thomas 'PointedEars' Lahn

gunso said:
How do i count number of saturdays and sundays in a function which
takes in 2 dates...

Three hints:

1. In ECMAScript implementations, dates are stored as objects.

2. How many days are there between today and next Saturday?

3. How many days are there between Saturday this week and
Saturday next week?
function countSatSun(startDate, endDate){

}

Should return me the total of saturdays and sundays between these
dates.

Yes. Unfortunately you forgot to post the function body, and your Shift key
is borken.

<http://jibbering.com/faq/#posting> pp.


PointedEars
 
J

John G Harris

Hi
How do i count number of saturdays and sundays in a function which
takes in 2 dates...

function countSatSun(startDate, endDate){

}

Should return me the total of saturdays and sundays between these
dates.

You have to decide what 'between' means. Does it include startDate and
endDate, or is it strictly between those dates?

John
 
L

Lasse Reichstein Nielsen

gunso said:
How do i count number of saturdays and sundays in a function which
takes in 2 dates...

As for all problems like this, first define, in words, what the function
is supposed to give.

In particular: Does the start and/or end date themselves count (if they are
Saturday or Sunday)?

Let's say, just to pick something, that end day is included, start day
is not, i.e., we want week-end days in the range ]start,end].
function countSatSun(startDate, endDate){

First we need the number of days between start and end.

var daysBetween = Math.round((endDate - startDate) / 864e5);

The number of whole weeks is easy:

var leftoverDays = daysBetween % 7;
var wholeWeeks = (daysBetween - leftoverDays) / 7;

Then the left-over days should be checked if they are Saturdays or
Sundays. We can do that by checking that the end days is within
"leftoverDays" since the last Saturday/Sunday.

// Days since last Sunday for end day.
var endDaysSinceSunday = endDate.getDay();

// Days since last Saturday for end day.
var endDaysSinceSaturday = (endDaysSinceSunday + 1) % 7;

return wholeWeeks * 2 + (leftoverDays > 0) *
((endDaysSinceSaturday < (leftoverDays - 1)) +
(endDaysSinceSunday < (leftoverDays - 1)));
}

Should return me the total of saturdays and sundays between these
dates.

Not thoroughly tested, use at own risk.

/L
 
D

Dr J R Stockton

In comp.lang.javascript message <b40e2144-3e6f-43f7-a20f-e4c181432167@r3
g2000vbp.googlegroups.com>, Sun, 10 May 2009 06:58:56, gunso
How do i count number of saturdays and sundays in a function which
takes in 2 dates...

function countSatSun(startDate, endDate){

}

Should return me the total of saturdays and sundays between these
dates.

Ignore T(PE)L. He does not understand English as it is, unfortunately,
commonly written; and may be under the illusion that he is a normal
human being.

You need to be more exact about your dates; is the time component zero,
and should you be thinking in UTC or LCT - the former is more efficient,
the latter can only be truly needed if real time is involved.

You will also need to know what "between" means; do you count the days
themselves, or are they necessarily weekdays.

If the dates are necessarily not too far apart and not too numerous, you
only need to step D through every day and count; weekends are when
D.get[UTC}Day()%6==0. It would be %6==1 for ISO 8601.

Otherwise, allowing for Summer Time if needed, one gets from a Date
Object to a daycount by dividing by 864e5; take the difference, integer
divide by 7, multiply by 2. Having dealt with complete weeks, just
consider the remaining fractional week.

<URL:http://www.merlyn.demon.co.uk/js-date7.htm> contains "Counting
Weekdays" etc., which should be sufficiently easy to adapt.

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.
 
J

Jorge

Hi
How do i count number of saturdays and sundays in a function which
takes in 2 dates...

function countSatSun(startDate, endDate){

}
(...)


function countSatSun (startDate, endDate) {

var numberOfDays= Math.floor((+endDate- +startDate)/(24*60*60*1e3));
var weekDay= endDate.getDay() || 7;

if (numberOfDays < 0) {
weekDay= startDate.getDay() || 7;
numberOfDays= -numberOfDays;
}

var ctrSaturdays, ctrSundays, weeks= Math.floor(numberOfDays / 7);
numberOfDays-= 7* (ctrSaturdays= ctrSundays= weeks);

do {
if (weekDay > 5) {
ctrSaturdays+= +(weekDay === 6);
ctrSundays+= +(weekDay === 7);
numberOfDays--;
weekDay--;
} else {
numberOfDays-= weekDay;
weekDay= 7;
}
} while (numberOfDays >= 0);

return ("Saturdays: "+ ctrSaturdays+ ", Sundays: "+ ctrSundays);
}



http://jorgechamorro.com/cljs/056/
 
D

Dr J R Stockton

In comp.lang.javascript message <c6705310-c11b-49cb-96f3-d6e6bd93a25a@m2
4g2000vbp.googlegroups.com>, Mon, 11 May 2009 00:37:54, Jorge

The OP has not actually said that the arguments are Date Objects; they
could be strings. Let us assume objects.

function countSatSun (startDate, endDate) {

var numberOfDays= Math.floor((+endDate- +startDate)/(24*60*60*1e3));

That is the integer part of the difference in UTC days. If both dates
have the same time part, then that numberOfDays will be affected by a
Summer Time transition.
var weekDay= endDate.getDay() || 7;

That, however, is the ISO 8601 form for the *LCT* day-of-week.


ISTM possibly best to convert both dates from LCT to UTC milliseconds
with the same Y M D :
ms = +startDate +- startDate.getTimezoneOffset()*6e4
and then to convert to days, with a Monday, earlier than either date,
being Day Zero :
dd = Math.floor(ms/864e5) + 3 // IIRC
which should be combined into a function.

Weekend = dd % 7 > 4

While incrementing an LCT date is moderately slow, incrementing a UTC
one will be faster, and incrementing a daycount faster still. So, for
what seem probable applications, it should be acceptable to increment
from one daycount to the other, counting weekend days as they pass. In
a second, my Firefox can from count to 1.5e6.
 
E

Evertjan.

Dr J R Stockton wrote on 11 mei 2009 in comp.lang.javascript:
In comp.lang.javascript message <c6705310-c11b-49cb-96f3- d6e6bd93a25a@m2
4g2000vbp.googlegroups.com>, Mon, 11 May 2009 00:37:54, Jorge
[......]

So, for
what seem probable applications, it should be acceptable to increment
from one daycount to the other, counting weekend days as they pass.


Forgetting about the date object for the moment,
if one can determine the number of included days
and the the weekday of day one:

<script type='text/javascript'>

function saturdaysPlusSundays(dayOne, numberOfDays) {
// Monday = 0

var numberOfExessDays = numberOfDays % 7;
var numberOfCompleteWeeks = (numberOfDays -numberOfExessDays) / 7;

var RestSaturdaysPlusSundays = numberOfExessDays + dayOne - 6;
RestSaturdaysPlusSundays =
Math.min(Math.max(RestSaturdaysPlusSundays,0),2);

return numberOfCompleteWeeks * 2 + RestSaturdaysPlusSundays;
};

// testing:
alert(saturdaysPlusSundays(4, 705));
alert(saturdaysPlusSundays(0, 705));
alert(saturdaysPlusSundays(4, 4));

In a second, my Firefox can from count to 1.5e6.

In a serverside js application that would mean serious degradation,
however.
 
R

Ricardo

function countSatSun (startDate, endDate) {

  var numberOfDays= Math.floor((+endDate- +startDate)/(24*60*60*1e3));
  var weekDay= endDate.getDay() || 7;

  if (numberOfDays < 0) {
    weekDay= startDate.getDay() || 7;
    numberOfDays= -numberOfDays;
  }

  var ctrSaturdays, ctrSundays, weeks= Math.floor(numberOfDays / 7);
  numberOfDays-= 7* (ctrSaturdays= ctrSundays= weeks);

  do {
    if (weekDay > 5) {
      ctrSaturdays+= +(weekDay === 6);
      ctrSundays+= +(weekDay === 7);
      numberOfDays--;
      weekDay--;
    } else {
      numberOfDays-= weekDay;
      weekDay= 7;
    }
  } while (numberOfDays >= 0);

  return ("Saturdays: "+ ctrSaturdays+ ", Sundays: "+ ctrSundays);

}

http://jorgechamorro.com/cljs/056/

Why overcomplicate?

function countRestDays(startDate, endDate){
var
totalDays = Math.round((endDate - startDate) / 86400000),
day = startDate.getDay(),
restDays = 0;

while (totalDays--){
if (++day > 6) day = 0;
restDays += !(day % 6);
}
return restDays;
}

This includes the ending day but not the starting one, easy to adjust
(change to post-increment to include the first, subtract 1 from total
to exclude last). If you need to squeeze more performance out of it
you can calculate the whole weeks and run the loop for the leftover
days only.
 
C

Chris Riesbeck

Lasse said:
As for all problems like this, first define, in words, what the function
is supposed to give.

In particular: Does the start and/or end date themselves count (if they are
Saturday or Sunday)?

My only quibble here would be with "define, in words" -- I recommend
"define, in runnable tests," e.g., with JSUnit if you like.
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>
In a serverside js application that would mean serious degradation,
however.

AFAIK, Firefox does not run serverside. The above is the direct result
of a test; readers can deduce that in a millisecond it could count to
1.5e3, and client-side a single millisecond is unimportant and 1500 days
is a long time.

But it is clear that server-side can use an arithmetic method, which can
be usefully checked in development against a counting method.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top