Home > Web Front-end > JS Tutorial > body text

Find in JavaScript whether January 1st is a Sunday in a range of years?

王林
Release: 2023-09-08 11:45:04
forward
1398 people have browsed it

在 JavaScript 中查找年份范围内的 1 月 1 日是星期日?

It's always important to know when January 1st falls on a Sunday over the years. This information may be used for a variety of purposes, such as scheduling events, managing projects, etc. The purpose of this article is to help you find in JavaScript the dates in a range of years where January 1st falls on a Sunday.

algorithm

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

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

method 1

This method uses a for loop to iterate over the range of years from the starting year to the ending year, and for each year, a new Date object is created containing the year, month (0-based index, so January is 0) and days(1) as parameters. Then use the getDay() method to retrieve the day of the week for January 1st of the current year (a 0-based index, so Sunday is 0). 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;
}
Copy after login

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);
Copy after login

Method 2

This method is similar to the first method, but with minor modifications. The dayOfWeek variable is calculated by adding 6 to the result of getDay() and then dividing by 7 using the modulo operator (%) to find the remainder. This ensures that the day of the week is always in the range 0 to 6, with 0 representing Sunday. If the dayOfWeek variable equals 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;
}
Copy after login

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);
Copy after login

in conclusion

In summary, finding January 1st as a Sunday in a range of years is a simple task in JavaScript using the algorithm discussed in this article. We introduce two different approaches with code and explanations, and also provide two worked examples to demonstrate the use of the algorithms. By using this algorithm you can easily find a range of years in which January 1st falls on a Sunday, which is useful for a variety of purposes.

The above is the detailed content of Find in JavaScript whether January 1st is a Sunday in a range of years?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!