Last updated: December 12, 2023.

To get the first date and day of the month, first create a new instance of the native Date object and set its date to the first day of the month:

const date = new Date(); // Creates new date object, default is current date

date.setDate(1); // Sets date to first day of month (count starts from 1)

date.toLocaleDateString('en-us'); // returns date e.g. 12/1/2023

You can then extract the day (as a number or string) and the date (in this case always 1) as follows:

date.getDay(); // returns 0-6, where 0 = Sunday and 6 = Saturday
date.toLocaleDateString('en-us', { weekday: 'long' }); // e.g. "Friday"

date.getDate(); // returns 1

To get the last weekday and date, you can use a Date object to navigate forward one month and then pass 0 into setDate(). This sets the date to the last day of the previous month (actually the current one since navigating forwards):

const date = new Date();

date.setMonth(date.getMonth() + 1); // increases month by 1
date.setDate(0); // date in month count starts from 1, 0 is first day of the previous month 

date.toLocaleDateString('en-us'); // returns date e.g. 12/31/2023

The day and date can then be extracted as before:

date.getDay(); // returns 0-6, where 0 = Sunday and 6 = Saturday
date.toLocaleDateString('en-us', { weekday: 'long' }); // e.g. "Sunday"

date.getDate(); // returns e.g. 31 (count starts from 1)

Share.
Leave A Reply