Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

98
Views
Create array of work week start dates (Mondays) based on the current date - 7 weeks ahead, and 2 weeks past (JavaScript)

I need to create a dropdown for a schedule system that lists the next 7 Mondays, and last 2 Mondays, based on the current date. The current date obviously could be any day of the week, but the list must only show Mondays.

As such, I also need YYYY-MM-DD formats for the values to be recorded, as well as "friendly" display formats to list in the dropdown.

So I need a JavaScript array of work week start dates (Mondays) based on the current date - 7 weeks ahead, and 2 weeks past. I can't find anything quite like this already on SO.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Here's what I've come up with to address this. It does not rely on any dependencies for formatting. It creates two arrays, which can then be used for the dropdown values:

let production_week_choices = []
const today = new Date(); // test with new Date('2022-01-01')
const day = today.getDay() // Today's current day-of-week (0=Sun ... 6=Sat) numerically
var display_format_options = { weekday: 'short', month: 'short', day: 'numeric' };
for (let i = -49; i < 14; i+=7) { // 7 weeks forward, 2 weeks back, increment loop by 7 days
    let i_monday = new Date(today.getFullYear(), today.getMonth(), today.getDate() - day + (day === 0 ? -6 : 1) - i); // adjust when day is Sunday
    let date_value = i_monday.toISOString().split('T')[0] // provide in YYYY-MM-DD string format
    let date_display = i_monday.toLocaleDateString("en-US", display_format_options) // provide in specified format
    production_week_choices.push({"Value": date_value, "Display": date_display})
}
console.log(production_week_choices)

Hope someone else finds this useful!

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!