Skip to content
Advertisement

Split weeks of month

I’m trying to get a week range for the month, I’ve somewhat accomplished what I’m looking for but I’d like the code to be more reliable. The data ranges are going to be used to some MySQL queries where I can filter out data by data range.

What I’d like to achieve is to get something like the Windows 10 calendar. So the result is

JavaScript

Windows 10 calendar example

CODE

JavaScript

Which results in

It’s missing the last two days of the month (30 and 31).

JavaScript

Advertisement

Answer

You can use DateTime objects to do this more readily, setting a start time as the first Monday on or before the start of the month, and then adding a week at a time until it is past the end of the month:

JavaScript

Output:

JavaScript

Demo on 3v4l.org

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement