Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6934431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:57:33+00:00 2026-05-27T11:57:33+00:00

i am writing a function in javascript that will return date array of all

  • 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:57:34+00:00Added an answer on May 27, 2026 at 11:57 am

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a Javascript function that would manipulate an array written on-the-fly and sent
I am writing a javascript function that takes a callback. The callback will be
I am writing a JavaScript function that can perform decimal place manipulation on a
I am trying to create a JavaScript object that will deal with all of
I'm writing something that will examine a function and rewrite that function in another
I am writing code that will use AJAX requests to get some HTML code
Can anyone suggest a pattern that can be used for writing a JavaScript API
I am writing a function that will sit in a external file that creates
I've been writing some jQuery functions that have JavaScript variables and looping, etc inside
I am writing a JavaScript function to highlight the row and column of a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.