I need help understanding Date() with a variable year

I getting really confused about how to use the Date function and all of the methods. I need to find the day of the week that November 1st falls for any year or just the current year. But I don’t know how to set the month to November and the day of the month to 1 and then use those values to get the day of the week for 11/1 for the current year.

I need to get the day of the week so that I can output the precincts in my township that have leaf pickup. Leaf pickup is over 6 weeks and there are 3 or 4 precincts per week. But each year takes the last 2 precincts from the year before and makes them the first two. I created that array using slice and push, but I want to output something like “Pickup for the week of November 1st…” and then “week of November 8th…”, etc.

I figured I would use switch for days 0 thru 6, but I either have to hard-code the date or I get the current date when I use new Date(). The leaf pickup is an 11-year cycle and then it repeats, and I also know that any date is on the same date every 7 years.

I think that I should get the current year, then grab the day of the week for 11/1 but Date() is confusing me. Any clues on how I should approach this?

What do you have so far? The Date() object has a method for getting the day of the week (.getDay()) that returns a number. Since the order of the days of the week is constant, you can use a static object to translate that number into text.

If you share your code, maybe we can better help you wrap your head around JavaScript dates.

I used getDay with a hard-coded date. Right now, I’m just messing around in CodePen. Here it is: https://codepen.io/jim-kernicky/pen/eYGNEON

But I realize that I will need an array of 11 arrays - for the 11 unique years - because I won’t be able to set the start date (this year). I think I’ll have to use a while loop to check for years past 2021.

I can create an array for the 11 possible precinct orderings, but then I need to create an array for the 6 Monday dates where the 1st week seems to be at the earliest November 1st. I say that because the Township puts out dates for the Monday each week, and this year 11/2 was actually on a Monday. So if I know what day of the week 11/1 falls on, then I can generate the date for week 1, and the 5 following dates would be week 1 + 7.

I’m not entirely sure what your end goal is, but I probably wouldn’t try to create a hard-coded array of the day 11/1 falls on every year. Assuming that you would want to look up some future year based on an input, you would figure out the information dynamically rather than just figuring it out in advance and storing it.

const WEEKDAYS = ['sunday','monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'];

const novemberFirstDay = (year) => WEEKDAYS[(new Date(`11/1/${year}`)).getDay()];

console.log(novemberFirstDay(2021)); // 'monday'

No, I wasn’t going to hard code any dates except for maybe this year as a start date. I’m hardcoded the sequence of precincts for the 11-year cycle, and then I want to output the weeks each precinct falls into based on the day of the week for Nov 1st. I like what you did there with novemberFirstDay. I was already working on variables to output the precincts and dates for each week. I should be done hopefully in a day or two - I’ll post a link to the finished CodePen - thanks!

I’m sure the US MM/D/YYYY format works fine on your machine, but please use the standard ISO format which is guaranteed to work on everyone’s!

11/1/2021 → 2021-11-01

Got it - right now I’m just trying to get the basic structure of what I want.

Cool! Congrats on making progress on what sounds like a fairly complex problem.

I was able to output list items for the precincts, but right now I am only outputting the date for the first Monday in November. I should have tried to get the first Monday and not necessarily the day of the week for the 1st. I used a switch statement to find the first Monday given the day of the week for Nov 1st.

Regardless, I have to figure out how to increment the date for the 1st Mon in Nov by a week for the following 5 weeks. And then somehow attach the correct date for each precinct. I inserted a bottom border to break up the list for each week. And I created a different pen: https://codepen.io/jim-kernicky/pen/PoJqzMd

So I have to figure out how to:

  1. increment the date by 7 days
  2. selectively attach a correct date for each precinct.

Good job so far!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.