Vanilla JavaScript get following Monday

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

In today's article, I wanted to check something very specific. Imagine we need to know the first upcoming Monday?
This can become very tricky because you will need to know if it's a new month or even a new year.
Luckily there is a quite easy way to do this.
So first we'll build the code based on whatever day it is you are reading this.
Then we will demo an end of month/year date.
Let's start by defining what today is:
let today = new Date();
Now we can make the wireframe of our function using an ES6 Arrow function.
getNextMonday = input => {
// Do something
return input;
};
The function is called getNextMonday, and it accepts one input. It then needs to do something with the input and return something.
So how do we go about finding the next Monday?
Let's modify the input we receive.
input.setDate(input.getDate() + ((8 - input.getDay()) % 7));
What we do here is set a new Date based on whatever the input was.
The input.getDate() will return 23 if you are reading this on the 23rd of December. Which is a Wednesday (3rd day of the week)
Then add the number of days till a Monday so in the case of the 23 it would be (23 + (8 - 3) % 7); = 28
Which happens to be a Monday! Yeey 🎉
Then we can return a template literal where we merge the dates.
return `The next monday is ${String(input.getDate()).padStart(2, '0')}-${String(
input.getMonth() + 1
).padStart(2, '0')}-${input.getFullYear()}`;
Quite a chunky one, but it fixes the dates by adding leading zeroes to our date using the padStart method.
The full function will then be:
getNextMonday = input => {
input.setDate(input.getDate() + ((8 - input.getDay()) % 7));
return `The next monday is ${String(input.getDate()).padStart(2, '0')}-${String(
input.getMonth() + 1
).padStart(2, '0')}-${input.getFullYear()}`;
};
Now, this worked since 23 + 5 = 28, but what if we go further than the number of days in a month?
Let's take New Years' eve 2020 which is on a Thursday again. The date is now 12-31-2020 (31st of December).
let today = new Date('12-31-2020');
Then if we run our function calculation the day will be?
(31 + (8 - 4) % 7); = 35
Which is weird right, there are only 31 days in December. But still, because we are doing these modifications on a Date object JavaScript understands it needs to count further.
That said the first Monday from New Years eve is:
P.s. it's The next Monday is 04-01-2021.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter