# JavaScript weekday list get

## All we need is an easy explanation of the problem, so here it is.

I need to create a JavaScript function for the below requirements. I need to get every week day date list. If you know nodeJs package tell me that. Thank you for your attention.

``````Example -
2022-01-03
2022-01-04
2022-01-05
2022-01-06
2022-01-07

2022-01-10
2022-01-11
2022-01-12
2022-01-13
2022-01-14
..........
..........
until year-end
``````

like this pattern (only Monday to Friday)

## How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

### Method 1

``````function getWeekDaysForYear(year) {
const isLeap = year % 4 === 0;
const numberOfDays = isLeap ? 366 : 365;
let currentDate = new Date(Date.UTC(year, 0, 0, 0, 0, 0, 0));

const weekDays = [];

for(let i = 1; i <= numberOfDays; i++) {
currentDate = new Date(currentDate.getTime() + 24 * 3600 * 1000);
if (currentDate.getDay() === 0 || currentDate.getDay() === 6) {
continue;
}
weekDays.push(currentDate.toISOString().split('T'));
}

return weekDays;
}
``````
``````console.log(getWeekDaysForYear(2022));
``````

This is a simple function which returns all the weekdays for the specified year in an array.

### Method 2

JavaScript’s `Date` object overflows, so you can use:

``````for (i=1;i<366;i++) {
if (i%7==1 || i%7==2) continue;
const d = new Date(2022, 0, i);
document.write(d.toDateString(),'<br>');
}``````

You will need to watch for leap years, and recalculate which days are the weekend every year.

### Method 3

Something like this

``````const endDate = new Date(2022,1,2);
const date = new Date(); //today

while (endDate > date) {
const weekDay = date.getDay();
if (weekDay != 6 && weekDay != 0) {
let year = new Intl.DateTimeFormat('en', { year: 'numeric' }).format(date);
let month = new Intl.DateTimeFormat('en', { month: '2-digit' }).format(date);
let day = new Intl.DateTimeFormat('en', { day: '2-digit' }).format(date);
console.log(`\${year}-\${month}-\${day}`);
}
date.setDate(date.getDate() + 1);
}
``````

### Method 4

Since we’re play code golf…

``````function getWeekDays(year) {
// Start on 1 Jan of given year
let d = new Date(Date.UTC(year, 0));
let result = [];
do {
// Only push dates that aren't Sat (6) or Sun (0)
d.getDay() % 6 ? result.push(d.toLocaleDateString('en-CA')) : null;
// Increment date
d.setDate(d.getDate() + 1);
// Until get to 1 Jan again
} while (d.getMonth() + d.getDate() > 1)
return result;
}

console.log(getWeekDays(new Date().getFullYear()))``````

### Method 5

``````function getWeekDaysForDateRange(start, end) {
const [startYear, startMonth, startDate] = start.split("-");
const [endYear, endMonth, endDate] = end.split("-");
let beginDate = new Date(Date.UTC(startYear, startMonth - 1, startDate - 1, 0, 0, 0, 0));
let closeDate = new Date(Date.UTC(endYear, endMonth - 1, endDate, 0, 0, 0, 0));

const weekDays = [];

while(beginDate.getTime() !== closeDate.getTime()) {
beginDate = new Date(beginDate.getTime() + 24 * 3600 * 1000);
if (beginDate.getDay() === 0 || beginDate.getDay() === 6) {
continue;
}
weekDays.push(beginDate.toISOString().split('T'));
}
return weekDays;
}

console.log(getWeekDaysForDateRange('2022-01-01', '2022-01-10'));
``````

Something like this would work for date range as you want!

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂