Get the week list of the year
function getWeekList() { const currentYear = new Date().getFullYear(); const firstDayOfYear = new Date(`${currentYear}-01-01`); const firstThursday = new Date(firstDayOfYear); ( () + ((4 - () + 7) % 7) + 1 ); const weekList = []; let currentWeek = 1; while (() === currentYear) { ({ value: currentWeek, text: `The${currentWeek}week` }); (() + 7); currentWeek++; } return weekList; } const weekList = getWeekList(); (weekList); /* Print result [ { value: 1, text: 'week 1' }, { value: 2, text: 'week 2' }, { value: 3, text: 'week 3' }, // ... Other weeks ] */
This function first gets the current year and then calculates the day of the week on which the first day of the year is. Calculate the number of weeks of that year based on the date of the first Thursday.
Then loop through, add the week number information to the weekList array and return. Each element is an object, containing value and text properties.
What week of the year is the current date
// Get the function of what week of the current date is in the yearfunction getWeekNumber(date) { // Set the firstDayOfWeek of the Date object to Monday (0, 0, 0, 0); // Set the firstDayOfYear of the Date object to January 1 (() + 4 - (() || 7)); // Calculate what week of the year the current date is const yearStart = new Date((), 0, 1); const weekNo = ((((date - yearStart) / 86400000) + 1) / 7); return weekNo; } // Get the current dateconst currentDate = new Date(); // Get the current yearconst currentYear = (); // Get the current date what week of the yearconst weekNumber = getWeekNumber(currentDate); // Output result(`${currentYear}Year${weekNumber}week`);
This code first gets the current date, and then calculates the week of the year through the getWeekNumber() function. The final output result is in the format of "XX Weekly of xx".
Note that the implementation of this function is based on the ISO 8601 standard, that is, the first week of the year starts from the first Thursday. If you need to use other defined week counting methods, you can modify them according to actual needs.
Summarize
This is the article about how to use js to obtain the weekly number list of the year and the current date is what weekly the current date is. For more related js to obtain the weekly number list of the year, please search for my previous article or continue browsing the related articles below. I hope everyone will support me in the future!