Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

JS Get day next month starts on

How can I get the day that the next month starts on? (Monday Tuesday etc.).

I tried the following but it returns more than just the day.

var date = new Date(), y = date.getFullYear(), m = date.getMonth();
var firstDay = new Date(y, m, 1);
var lastDay = new Date(y, m + 1, 0);

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You’re almost there, just add this after your last line of code to get the name of the day

var dayName = lastDay.toLocaleDateString('en-us', { weekday: 'long' }); 

Also keep in mind that Date constructor’s second parameter is monthIndex MDN.

You should add m by 1 to get next month’s first day and add it by 2 and provide date = 0 to get the last day of the next month.

var date = new Date(), y = date.getFullYear(), m = date.getMonth();
var firstDay = new Date(y, m + 1, 1);
var lastDay = new Date(y, m + 2, 0);
var firstDayName = firstDay .toLocaleDateString('en-us', { weekday: 'long' }); 
var lastDayName = lastDay.toLocaleDateString('en-us', { weekday: 'long' }); 
console.log(firstDayName, lastDayName)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading