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

Finding years where January 1st falls on a Sunday is useful for planning events, scheduling, and calendar calculations. JavaScript's Date object makes this straightforward by providing methods to determine the day of the week.

How It Works

The approach uses JavaScript's Date constructor and getDay() method. The getDay() method returns 0 for Sunday, 1 for Monday, and so on. We iterate through a range of years, create a Date object for January 1st of each year, and check if it's a Sunday.

Method 1: Direct Sunday Check

This method directly checks if getDay() returns 0 (Sunday):

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

let startYear = 2000;
let endYear = 2030;
let sundays = findFirstSunday(startYear, endYear);
console.log("Years where Jan 1st is Sunday:", sundays);
Years where Jan 1st is Sunday: [ 2006, 2012, 2017, 2023, 2028 ]

Method 2: Modular Arithmetic Approach

This method uses modular arithmetic, though it's unnecessarily complex for this simple case:

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

let sundays2 = findFirstSundayModular(2020, 2040);
console.log("Using modular approach:", sundays2);
Using modular approach: [ 2023, 2028, 2034, 2040 ]

Enhanced Version with Day Names

Here's a more informative version that shows the actual day names:

function findJanuaryFirstDetails(startYear, endYear) {
   const days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
   let results = [];
   
   for (let year = startYear; year <= endYear; year++) {
      let date = new Date(year, 0, 1);
      let dayIndex = date.getDay();
      let dayName = days[dayIndex];
      
      if (dayIndex === 0) { // Sunday
         results.push({year, day: dayName});
      }
   }
   return results;
}

let details = findJanuaryFirstDetails(2024, 2030);
console.log("Detailed results:");
details.forEach(item => {
   console.log(`${item.year}: January 1st is a ${item.day}`);
});
Detailed results:
2028: January 1st is a Sunday

Comparison of Methods

Method Complexity Readability Performance
Direct Sunday Check Simple High Fast
Modular Arithmetic Unnecessary Lower Slightly slower

Key Points

  • new Date(year, 0, 1) creates January 1st for the given year
  • getDay() returns 0-6, where 0 is Sunday
  • The pattern repeats approximately every 28 years due to leap year cycles
  • Method 1 is preferred for its simplicity and clarity

Conclusion

Finding years where January 1st falls on Sunday is straightforward using JavaScript's Date object. The direct approach checking getDay() === 0 is the most readable and efficient method for this task.

Updated on: 2026-03-15T23:19:01+05:30

654 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements