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

javascript function return date array for all Sundays in a year - Stack Overflow

programmeradmin2浏览0评论

i am writing a function in javascript that will return date array of all sundays. below you can see my code :

function getDefaultOffDays(year){
var offdays=new Array();
i=0;
for(month=1;month<12;month++)
{
    tdays=new Date(year, month, 0).getDate();

    for(date=1;date<=tdays;date++)
    {
        smonth=(month<10)?"0"+month:month;
        sdate=(date<10)?"0"+date:date;
        dd=year+"-"+smonth+"-"+sdate;

        day=new Date();
        day.setDate(date);
        day.setMonth(month);
        day.setFullYear(year);

        if(day.getDay() == 0 )
             {              
               offdays[i++]=dd;

             }
    }
}

return offdays;
}

the issue is that the returned array is giving random dates not the only dates for sunday :( m i missing some thing?

i am writing a function in javascript that will return date array of all sundays. below you can see my code :

function getDefaultOffDays(year){
var offdays=new Array();
i=0;
for(month=1;month<12;month++)
{
    tdays=new Date(year, month, 0).getDate();

    for(date=1;date<=tdays;date++)
    {
        smonth=(month<10)?"0"+month:month;
        sdate=(date<10)?"0"+date:date;
        dd=year+"-"+smonth+"-"+sdate;

        day=new Date();
        day.setDate(date);
        day.setMonth(month);
        day.setFullYear(year);

        if(day.getDay() == 0 )
             {              
               offdays[i++]=dd;

             }
    }
}

return offdays;
}

the issue is that the returned array is giving random dates not the only dates for sunday :( m i missing some thing?

Share Improve this question edited Dec 15, 2011 at 6:57 Bhesh Gurung 51k23 gold badges95 silver badges143 bronze badges asked Dec 15, 2011 at 6:32 Sadaf SidSadaf Sid 1,5703 gold badges18 silver badges32 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 13

If you examine the result, you can see that it's actually not random. It returns the dates for january that are sundays in february, and so on.

The month property of the Date object is zero based, not one based. If you change this line, the function will return the correct dates:

day.setMonth(month - 1);

Also, the loop only runs from 1 to 11, you need to include december too:

for (month=1; month <= 12; month++)

Another way to do this would be to find the first sunday, then just step forward seven days at a time:

function getDefaultOffDays2(year) {
  var date = new Date(year, 0, 1);
  while (date.getDay() != 0) {
    date.setDate(date.getDate() + 1);
  }
  var days = [];
  while (date.getFullYear() == year) {
    var m = date.getMonth() + 1;
    var d = date.getDate();
    days.push(
      year + '-' +
      (m < 10 ? '0' + m : m) + '-' +
      (d < 10 ? '0' + d : d)
    );
    date.setDate(date.getDate() + 7);
  }
  return days;
}

One bug:

for(month=1;month<12;month++)

That's just 11 months.

If you want for the whole year you need:

for(month=0;month<12;month++)

Because there are 12 months in a year. You can even bine that with Guffa's answer.

Month is zero based as like day, so if month is 0 then it Jan, So changed your code as below,

function getDefaultOffDays(year){ 
var offdays=new Array();
i=0;
for(month=0;month<12;month++) { 
    tdays=new Date(year, month, 0).getDate(); 
    for(date=1;date<=tdays;date++)     {
        smonth=(month<10)?"0"+(month+1):(month+1);
        sdate=(date<10)?"0"+date:date;
        dd=year+"-"+smonth+"-"+sdate;
        var day=new Date(year,month,date);
        if(day.getDay() == 0 )              {
            offdays[i++]=dd;
        }
      }
    }
    return offdays; 
  }

This function has error error code:

tdays=new Date(year, month, 0).getDate(); 

replace to:

tdays=new Date(year, month, 1).getDate(); 

becouse 0(day) return prev month

发布评论

评论列表(0)

  1. 暂无评论