Find 1st January be Sunday between a range of years in JavaScript?


It's always important to know when the 1st January falls on a Sunday in a range of years. This information can be used for various purposes, such as scheduling events, managing projects, and more. The purpose of this article is to help you find the 1st January falling on Sunday in a range of years in JavaScript.

Algorithm

The algorithm to find the 1st January falling on Sunday between a range of years involves several steps. The first step is to calculate the number of days between the current year and the year you want to find the 1st January falling on Sunday. The second step is to find the day of the week for the 1st January of the current year, and the third step is to find the number of days between the 1st January of the current year and the 1st January falling on Sunday in the range of years.

The importance of this algorithm lies in its simplicity and efficiency. With this algorithm, you can quickly and easily find the 1st January falling on Sunday in a range of years.

Approach 1

This approach uses a for loop to iterate through the range of years from the start year to the end year, and for each year, a new Date object is created with the year, month (0-based index, so January is 0), and day (1) as parameters. The getDay() method is then used to retrieve the day of the week (0-based index, so Sunday is 0) for the 1st January of the current year. If the day of the week is 0, the year is added to the firstSunday array. Finally, the firstSunday array is returned as the result.

function findFirstSunday(startYear, endYear) {
   let firstSunday = [];
   for (let year = startYear; year <= endYear; year++) {
      let date = new Date(year, 0, 1);
      if (date.getDay() === 0) {	
         firstSunday.push(year);
      }
   }
   return firstSunday;
}

Example 1

function findFirstSunday(startYear, endYear) {
   let firstSunday = [];
   for (let year = startYear; year <= endYear; year++) {
      let date = new Date(year, 0, 1);
      if (date.getDay() === 0) {	
         firstSunday.push(year);
      }
   }
   return firstSunday;
}
let startYear = 2000;
let endYear = 2050;
let firstSunday = findFirstSunday(startYear, endYear);
console.log(firstSunday);

Approach 2

This approach is similar to the first approach, but with a small modification. The dayOfWeek variable is calculated by adding 6 to the result of getDay() and then using the modulus operator (%) to find the remainder after dividing by 7. This ensures that the day of the week is always in the range of 0 to 6, with 0 representing Sunday. If the dayOfWeek variable is equal to 0, the year is added to the firstSunday array.

function findFirstSunday(startYear, endYear) {
   let firstSunday = [];
   for (let year = startYear; year <= endYear; year++) {
      let dayOfWeek = (new Date(year, 0, 1).getDay() + 6) % 7;
      if (dayOfWeek === 0) {
         firstSunday.push(year);
      }
   }
   return firstSunday;
}

Example 2

function findFirstSunday(startYear, endYear) {
   let firstSunday = [];
   for (let year = startYear; year <= endYear; year++) {
      let dayOfWeek = (new Date(year, 0, 1).getDay() + 6) % 7;
      if (dayOfWeek === 0) {
         firstSunday.push(year);
      }
   }
   return firstSunday;
}
let startYear = 1990;
let endYear = 2020;
let firstSunday = findFirstSunday(startYear, endYear);
console.log(firstSunday);

Conclusion

In conclusion, finding the 1st January falling on Sunday in a range of years in JavaScript is a straightforward task with the algorithm discussed in this article. We covered two different approaches with code and explanations, and two working examples were also provided to demonstrate the usage of the algorithm. By using the algorithm, you can easily find the 1st January falling on Sunday in a range of years, which can be useful for various purposes.

Updated on: 17-Apr-2023

188 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements