最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to count the number of sundays between two dates - Stack Overflow

programmeradmin2浏览0评论

I tried the JS below:

var start = new Date("25-05-2016");
var finish = new Date("31-05-2016");
var dayMilliseconds = 1000 * 60 * 60 * 24;
var weekendDays = 0;
while (start <= finish) {
  var day = start.getDay()
  if (day == 0) {
    weekendDays++;
  }
  start = new Date(+start + dayMilliseconds);
}
alert(weekendDays);

I tried the JS below:

var start = new Date("25-05-2016");
var finish = new Date("31-05-2016");
var dayMilliseconds = 1000 * 60 * 60 * 24;
var weekendDays = 0;
while (start <= finish) {
  var day = start.getDay()
  if (day == 0) {
    weekendDays++;
  }
  start = new Date(+start + dayMilliseconds);
}
alert(weekendDays);

However, it gives the wrong output.

I need to get the total count of Sundays between the two dates.

Share Improve this question edited Aug 8, 2018 at 10:29 vishuB asked May 24, 2016 at 7:49 vishuBvishuB 4,2615 gold badges35 silver badges50 bronze badges 3
  • Where here in PHP question? The question does not apply to PHP – Maxim Tkach Commented May 24, 2016 at 7:54
  • Given code is no where close to what you need....;) – Jai Commented May 24, 2016 at 7:55
  • You don't need a loop to do this! – Quentin Roy Commented May 24, 2016 at 9:14
Add a comment  | 

8 Answers 8

Reset to default 5

You use incorrect date format.It will work if init date so:

var start = new Date("2016-05-25");
var finish = new Date("2016-05-31");

Your date format is wrong. Dates' string format is "yyyy-mm-dd". See here for more information.

Also, looping each day of the interval is very inefficient. You may try the following instead.

function getNumberOfWeekDays(start, end, dayNum){
  // Sunday's num is 0 with Date.prototype.getDay.
  dayNum = dayNum || 0;
  // Calculate the number of days between start and end.
  var daysInInterval = Math.ceil((end.getTime() - start.getTime()) / (1000 * 3600 * 24));
  // Calculate the nb of days before the next target day (e.g. next Sunday after start).
  var toNextTargetDay = (7 + dayNum - start.getDay()) % 7;
  // Calculate the number of days from the first target day to the end.
  var daysFromFirstTargetDay = Math.max(daysInInterval - toNextTargetDay, 0);
  // Calculate the number of weeks (even partial) from the first target day to the end.
  return Math.ceil(daysFromFirstTargetDay / 7);
}


var start = new Date("2016-05-25");
var finish = new Date("2016-05-31");

console.log("Start:", start);
console.log("Start's week day num:", start.getDay());
console.log("Finish:", finish);
console.log("Finish's week day num:", finish.getDay());

console.log("Number of Sundays:", getNumberOfWeekDays(start, finish));

Your date format and comparison condition should change like the following:

var start = new Date("2016-05-11");
  var finish = new Date("2016-05-31");
  var dayMilliseconds = 1000 * 60 * 60 * 24;
  var weekendDays = 0;
  while (start.getTime() <= finish.getTime()) {
    var day = start.getDay();
    if (day == 0) {
        weekendDays++;
    }
    start = new Date(+start + dayMilliseconds);
  }
  alert(weekendDays);

Check Fiddle

You are using incorrect date format.
Just Change the format to:

var start = new Date(2016, 4, 25);
var finish = new Date(2016, 4, 31);

Try this function:

  function CalculateWeekendDays(fromDate, toDate){
        var weekendDayCount = 0;
        while(fromDate < toDate){
            fromDate.setDate(fromDate.getDate() + 1);
            if(fromDate.getDay() === 0){
                ++weekendDayCount ;
            }
        }
        return weekendDayCount ;
    }


console.log(CalculateWeekendDays(new Date(2011, 6, 2), new Date(2011, 7, 2)));

This will give you number of sunday come between 2 dates

change your date format.It will work

var start = new Date("05-16-2016");
  var finish = new Date("05-31-2016");
  var dayMilliseconds = 1000 * 60 * 60 * 24;
  var weekendDays = 0;
  while (start <= finish) {
    var day = start.getDay()
    if (day == 0) {
        weekendDays++;
    }
    start = new Date(+start + dayMilliseconds);
  }
  console.log(weekendDays);

JS date format doesn't have "dd-MM-yyyy" ,so it will invalid date format .Try recreate date is ok or just change your date format Date Format

Try this:

var start = new Date("25-05-2016");
var end = new Date("31-05-2016");

var startDate = new Date(start);
var endDate = new Date(end);
var totalSundays = 0;

for (var i = startDate; i <= endDate; ){
    if (i.getDay() == 0){
        totalSundays++;
    }
    i.setTime(i.getTime() + 1000*60*60*24);
}

console.log(totalSundays);
// Find date of sundays b/w two dates
var fromDate = new Date('2022-10-26')
var toDate = new Date('2022-11-31')
var sunday = 0
var milisec = 1000 * 60 * 60 * 24;
while (fromDate <= toDate) {
    var day = fromDate.getDay()
    if (day == 0) {
        sunday++
        console.log('Date of sunday:', fromDate)
    }
    fromDate = new Date(+fromDate + milisec)
}
console.log('Total no. of sundays:', sunday)
发布评论

评论列表(0)

  1. 暂无评论