0

I am trying to get how many saturdays and sundays exist between two dates.

I get the first date from a input date field

<input value="" type="date" name="exit_end_document" id="exit_end_document" class="form-control" required>

My Javascript is this:

 var fromDate = $('#exit_end_document').val();

I am getting the value.. the problem is that i do not know how can i calculate between that date which i get from input date field and today.

I have seen many examples but none of them do this...

(input date field) 2019-03-01 to (This date comes directly from JS) 2019-03-05 result = 2

Thanks!

  • They key is to look for a solution that does something similar and to then adapt it to your needs. –  Mar 05 '19 at 20:43
  • There aren't two weekends between 2019-03-01 and 2019-03-05. Do you really want the total number of Saturdays and Sundays in the given range? – The Head Rush Mar 05 '19 at 20:46
  • Yes I fixed it you were right – Inversiones Cova Codes CA Mar 05 '19 at 20:49
  • https://mssharepointworld.wordpress.com/2012/07/03/calculate-weekends-between-two-dates-using-javascript/ – Zak Mar 05 '19 at 20:50
  • @Zak the problem with that example is that it has the dates directly added var date1 = new Date(’06/04/2012′); var date2 = new Date(’08/17/2012′); how can i put the date that i am getting from the input field, and the date from today? – Inversiones Cova Codes CA Mar 05 '19 at 20:52
  • @Inversiones Cova Codes CA --- Sooooo Replace the `var` with your own input etc ... If you can't get that far, then you're just asking for a tutorial which is against SO guidelines https://stackoverflow.com/help/mcve – Zak Mar 05 '19 at 20:55
  • @Zak I replace this way var fromDate = $('#exit_end_document').val(); var date1 = new Date( fromDate); var date2 = new Date(); and it does not work – Inversiones Cova Codes CA Mar 05 '19 at 20:57
  • Look at the date formats .. Are they exactly the same? – Zak Mar 05 '19 at 20:58
  • Possible duplicate of [Calculate number of specific weekdays between dates](https://stackoverflow.com/questions/25562173/calculate-number-of-specific-weekdays-between-dates) – Heretic Monkey Mar 05 '19 at 21:25
  • @Zak you gave me the answer hehe Thanks man! :D – Inversiones Cova Codes CA Mar 05 '19 at 21:26
  • I gave you the way to FIND the answer .. You came up with the solution yourself ;-) – Zak Mar 05 '19 at 21:32

3 Answers3

0

In order to handle leap years and timezones and whatnot, i suggest testing all the between days and testing them to see if they are sat or sunday:

var date1 = new Date("2012-06-04T05:00:00.000Z"); 
var date2 = new Date("2012-08-17T05:00:00.000Z"); 
var weekendDays = 0;
for(var i = +date1, mx = +date2; i<mx; i+=(1000*60*60*24)){ 
   if({0:1,6:1}[new Date(i).getDay()]) weekendDays++;
}

alert(weekendDays); // 20
dandavis
  • 16,370
  • 5
  • 40
  • 36
  • The behavior of `Date(string)`, besides some formats from ISO 8601, is implementation dependent. Care should be taken when using it. – Heretic Monkey Mar 05 '19 at 21:19
  • @HereticMonkey: for sure. OP's source format was an ``, which should output a parse-able format though. I would add timezone info to it as well, though technically, if all the calcs are made in the local timezone, the offsets should cancel out, i think... – dandavis Mar 05 '19 at 21:19
  • I feel that the less we show even sample code using `Date(string)`, the better. – Heretic Monkey Mar 05 '19 at 21:23
  • @HereticMonkey. i tested in ie+ch+ff and it parsed, but point conceded; i changed my example format to ISO. I've actually seen more date problems on here by using a list of numbers ex `new Date(y,m,d...)` because of JS's stupid zero-january thing, but string also have issues. I think they are re-doing the dates for a future ES, hopefully it will be something good... – dandavis Mar 05 '19 at 21:25
0

Let's analyze this mathematically.

The starting date can either be on a Saturday or not. Likewise, the ending date can be either on a Saturday or not. In the simplest case, both dates are on Saturday; then you can see clearly that the number of Saturdays is equal to 1 plus the number of weeks between the two dates.

From there, it's easy to see that if the starting date is on a Saturday, but the ending date is not, then the number of Saturdays is equal to 1 plus the number of weeks between the two dates rounded down since the ending date's week has not reached Saturday yet. Turns out, that same math works for the first example, too, since you'll have an integer number of weeks between the dates. So we can cover both examples by simply using 1 + floor(weeks_between_dates) .

What if the ending date is a Saturday, but the starting date is not? Turns out, the math still works the same! This is the same as "moving back" the starting date from its Saturday, and that will add a partial week until it reaches the previous Saturday. Those partial weeks get rounded out by the floor, and once it reaches the previous Saturday, you'll be adding 1 anyway, as it'll be a full week added to the difference! So we're still good with 1 + floor(weeks_between_dates).

So the only possible combination left are two dates which are both not Saturday. This is the most complicated possibility. Let's start simple and assume the dates are two consecutive Wednesdays. Then they are 1 week apart and have 1 Saturday between them. Simple. If they're two weeks apart, they have 2 Saturdays. But what if it's a Wednesday and the following Tuesday? There is less than a week, but still 1 Saturday between them. And if it's a Wednesday and the following Thursday? More than 1 week, but still 1 Saturday! So in this case, we'd want to round the number of weeks up and stop there, giving us ceil(weeks_between_dates). But if they're both in the same week -- for instance, a Monday and a Friday in the same week -- then the answer is just 0. So how do we know whether the days are part of the same week? Assuming they're sorted and the start date is always before the ending date, then they're in the same week if and only if there is fewer than 1 week between them AND the starting weekday is before the ending weekday.

So the straight conditional logic here is this (in pseudocode):

weeks_between = floor((days between start and end) / 7)
if start.weekday = Saturday or end.weekday = Saturday, then:
    return 1 + weeks_between
else if weeks_between = 0 and start.weekday is before end.weekday, then:
    return 0
else
  return ceil((days between start and end) / 7)
IceMetalPunk
  • 5,476
  • 3
  • 19
  • 26
  • No, it's not. I didn't specify how you calculate the number of days between starting and ending. That's the calculation that will account for leap year; it'll add 1 to the difference if you cross the February-March border on a leap year. – IceMetalPunk Mar 05 '19 at 21:21
0

I already found the solution and it was given from @zak:

var fromDate = $('#exit_end_document').val();

fromDate = new Date(fromDate);

toDate = new Date();

var weekendDays = 0;

dayMilliseconds = 1000 * 60 * 60 * 24;

date1 = fromDate;

date2 = toDate;

while (date1 <= date2) {
  var day = date1.getDay();

  if (day == 0 || day == 6) {
    weekendDays++;
  }

  date1 = new Date(+date1 + dayMilliseconds);
}

alert(weekendDays);