Get the First and Last Date of a Month in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 3, 2024
7 min

banner

# Table of Contents

  1. Get the First and Last Date of a Month in JavaScript
  2. Get First and Last day of the Previous Month in JavaScript

# Get the First and Last Date of a Month in JavaScript

To get the first and last Date of a month:

  1. Use the Date() constructor to create a date object.
  2. Pass it the year, month and 1 for the day to get the first day of the month.
  3. Pass it the year, month and 0 for the day to get the last day of the month.

Here is a function that returns the first day of the month.

index.js
function getFirstDayOfMonth(year, month) { return new Date(year, month, 1); } // โœ… Get the first day of the current month const date = new Date(); const firstDay = getFirstDayOfMonth( date.getFullYear(), date.getMonth(), ); console.log(firstDay); // ------------------------------------------------------ // โœ… Get the first day of any month const firstDayJanuary = getFirstDayOfMonth(2025, 0); console.log(firstDayJanuary); // ๐Ÿ‘‰๏ธ Wed Jan 01 2025

get first date of month

The code for this article is available on GitHub

And here is a function that returns the last day of the month.

index.js
function getLastDayOfMonth(year, month) { return new Date(year, month + 1, 0); } // โœ… Get the last day of current month const date = new Date(); const lastDayCurrentMonth = getLastDayOfMonth( date.getFullYear(), date.getMonth(), ); console.log(lastDayCurrentMonth); // โœ… Get the last day of ANY month const lastDayJan = getLastDayOfMonth(2025, 0); console.log(lastDayJan); // ๐Ÿ‘‰๏ธ Fri Jan 31 2025

get last day of month

The code for this article is available on GitHub

You can also use the following code sample if you only need to get the first and last day of the current month.

index.js
const now = new Date(); const firstDay = new Date(now.getFullYear(), now.getMonth(), 1); console.log(firstDay); // ๐Ÿ‘‰๏ธ Sun Jan 01 2023 ... const lastDay = new Date(now.getFullYear(), now.getMonth() + 1, 0); console.log(lastDay); // ๐Ÿ‘‰๏ธ Tue Jan 31 2023 ...

We passed the following 3 arguments to the Date():

  1. The year
  2. The month
  3. The day
index.js
function getFirstDayOfMonth(year, month) { return new Date(year, month, 1); }

The Date.getFullYear() method returns the year of the date.

index.js
console.log(new Date().getFullYear()); // ๐Ÿ‘‰๏ธ 2023 console.log(new Date().getMonth()); // ๐Ÿ‘‰๏ธ 0 (January)

The Date.getMonth() method returns the month of the given date.

The method returns an integer from 0 (January) to 11 (December).

Months are zero-based in JavaScript, so 0 is January and 11 is December.

We hardcoded 1 for the day to get the first day of the month.

# Getting the first day of the next month

Here is an example of how to get the first day of next month.

index.js
function getFirstDayOfMonth(year, month) { return new Date(year, month, 1); } const date = new Date(); const firstDayNextMonth = getFirstDayOfMonth( new Date().getFullYear(), date.getMonth() + 1, ); // ๐Ÿ‘‡๏ธ Tue Aug 01 2023 console.log(firstDayNextMonth);

getting the first day of next month

The code for this article is available on GitHub

We added 1 to the output of the date.getMonth() method to get the next month.

The function returned the first day of the next month based on the supplied year and month values.

To get the last day of the month:

  1. We add 1 to the value of the month because we want to get a value representing the next month
  2. Then we roll back 1 day by specifying 0 for the day parameter.
index.js
function getLastDayOfMonth(year, month) { return new Date(year, month + 1, 0); }
Going 1 month forward and 1 day back gets us the last day of the specified month.

The getFirstDayOfMonth and getLastDayOfMonth functions can be used to get the first day of any month, here are some examples.

index.js
function getFirstDayOfMonth(year, month) { return new Date(year, month, 1); } console.log(getFirstDayOfMonth(2026, 0)); // ๐Ÿ‘‰๏ธ Thu Jan 01 2026 console.log(getFirstDayOfMonth(2031, 0)); // ๐Ÿ‘‰๏ธ Wed Jan 01 2031 console.log(getFirstDayOfMonth(2036, 0)); // ๐Ÿ‘‰๏ธ Tue Jan 01 2036 // ------------------------------------------------------------ function getLastDayOfMonth(year, month) { return new Date(year, month + 1, 0); } console.log(getLastDayOfMonth(2027, 0)); // ๐Ÿ‘‰๏ธ Sun Jan 31 2027 console.log(getLastDayOfMonth(2028, 1)); // ๐Ÿ‘‰๏ธ Tue Feb 29 2028 console.log(getLastDayOfMonth(2029, 2)); // ๐Ÿ‘‰๏ธ Sat Mar 31 2029
The code for this article is available on GitHub

The tricky thing to remember is that months are zero-based and go from 0 (January) to 11 (December).

This gets handled when you use the getMonth method as it returns an integer from 0 to 11.

If you want to make your code more readable, extract the value for the month into a variable.

index.js
function getFirstDayOfMonth(year, month) { return new Date(year, month, 1); } const january = 0; console.log(getFirstDayOfMonth(2026, january)); // ๐Ÿ‘‰๏ธ Thu Jan 01 2026

We initialized the january variable to 0 and used the variable in the call to the getFirstDayOfMonth method.

This makes our code a bit more readable and intuitive.

# Getting the last day of the next month

If you need to get the last day of next month, use the getFullYear() and getMonth() methods.

index.js
function getLastDayOfMonth(year, month) { return new Date(year, month + 1, 0); } const date = new Date(); const lastDayNextMonth = getLastDayOfMonth( date.getFullYear(), date.getMonth() + 1, ); console.log(lastDayNextMonth); // ๐Ÿ‘‰๏ธ Thu Aug 31 2023

getting the last day of the next month

The code for this article is available on GitHub

We added 2 to the output of the getMonth() method to get the month after next month.

This is balanced out because we passed 0 as the days parameter to the Date() constructor.

Specifying a day of 0 means "give me the last day of the prior month".

We go 2 months forward by adding 2 to the return value of the getMonth method and then go 1 day back to the last day of the next month by specifying 0 in the days slot.

This would also work if the next month is January. Then, The year would get rolled over and we would still get the correct date.

index.js
// ๐Ÿ‘‡๏ธ Sun Jan 31 2027 console.log(new Date(2026, 11 + 2, 0));

We passed 2026 as the year to the Date() constructor.

We specified 11 (December) + 2 for the month to get the month of February and we rolled back 1 day by setting the day to 0.

As expected, this gives us the last day of January 2027.

Our solution still works if the next month is January of the next year because of how dates work in JavaScript

# Get First and Last day of the Previous Month in JavaScript

You can also use the Date() constructor to get the first and last day of the previous month.

index.js
// โœ… Get the first day of the previous month function getFirstDayPreviousMonth() { const date = new Date(); return new Date(date.getFullYear(), date.getMonth() - 1, 1); } console.log(getFirstDayPreviousMonth()); // ๐Ÿ‘‰๏ธ Tue 1st December // ------------------------------------------------ // โœ… Get the last day of the previous month function getLastDayPreviousMonth() { const date = new Date(); return new Date(date.getFullYear(), date.getMonth(), 0); } console.log(getLastDayPreviousMonth()); // ๐Ÿ‘‰๏ธ Sun 31st December
The code for this article is available on GitHub

We passed the following 3 arguments to the Date():

  1. year - a 4-digit integer value representing the year of the date.
  2. monthIndex - an integer value from 0 (January) to 11 (December) that represents the month.
  3. day - an integer value representing the day of the month.

# Get the first day of the previous month in JavaScript

To get the first day of the previous month, we had to subtract 1 from the return value of the getMonth() method.

index.js
const date = new Date('2023-02-04'); const firstDayPrevMonth = new Date( date.getFullYear(), date.getMonth() - 1, 1 ); // ๐Ÿ‘‡๏ธ Sun Jan 01 2023 00:00:00 console.log(firstDayPrevMonth);

We subtracted 1 from the output of the getMonth() method to roll back into the previous month.

We hardcoded 1 for the day of the month value to get the first day of the previous month.

# Get the last day of the previous month in JavaScript

The Date object in JavaScript automatically handles the scenario where the year has to be adjusted, e.g. if we are in January, we have to roll back to December of the previous year.

index.js
const date = new Date('2022-01-17'); const firstDayPrevMonth = new Date( date.getFullYear(), date.getMonth() - 1, 1 ); // ๐Ÿ‘‡๏ธ Wed Dec 01 2021 00:00:00 console.log(firstDayPrevMonth); const lastDayPrevMonth = new Date( date.getFullYear(), date.getMonth(), 0 ); // ๐Ÿ‘‡๏ธ Fri Dec 31 2021 00:00:00 console.log(lastDayPrevMonth);
The code for this article is available on GitHub

We got the first and last days of the previous month with a Date object that stores a date in January.

The Date object automatically updated the year when we subtracted 1 from the month index of January.

To get the last day of the previous month, we simply had to pass 0 as the day of the month.

index.js
const date = new Date('2022-01-17'); const lastDayPrevMonth = new Date( date.getFullYear(), date.getMonth(), 0 ); // ๐Ÿ‘‡๏ธ Fri Dec 31 2021 00:00:00 console.log(lastDayPrevMonth);

The Date object automatically handles this by:

  • rolling back the month
  • setting the day of the month to the last day

As the code sample shows, the Date object also handles the scenario where the year has to be adjusted.

We used the Date.getFullYear() method to get the current year.

index.js
console.log(new Date().getFullYear()); // ๐Ÿ‘‰๏ธ 2023 console.log(new Date(2027, 0, 24).getFullYear()); // ๐Ÿ‘‰๏ธ 2027

We used the Date.getMonth() method to get the current month.

The getMonth method returns an integer from 0 (January) to 11 (December).

index.js
console.log(new Date().getMonth()); // ๐Ÿ‘‰๏ธ 0 (January)
Months are zero-based in JavaScript and go from 0 (January) to 11 (December).

Define two reusable functions to not have to remember the intricacies of how dates work in JavaScript.

index.js
// โœ… Get the first day of the previous month function getFirstDayPreviousMonth(date = new Date()) { return new Date(date.getFullYear(), date.getMonth() - 1, 1); } // ๐Ÿ‘‡๏ธ Tue 1st December 2022 console.log(getFirstDayPreviousMonth()); // ๐Ÿ‘‡๏ธ Sun 1st January 2023 console.log(getFirstDayPreviousMonth(new Date(2023, 1, 24))); // ------------------------------------------------ // โœ… Get the last day of the previous month function getLastDayPreviousMonth(date = new Date()) { return new Date(date.getFullYear(), date.getMonth(), 0); } // ๐Ÿ‘‡๏ธ Sun 31st December 2022 console.log(getLastDayPreviousMonth()); // ๐Ÿ‘‡๏ธ Tue 31st January 2023 console.log(getLastDayPreviousMonth(new Date(2023, 1, 24)));
The code for this article is available on GitHub

The getFirstDayPreviousMonth function takes a date object and returns the first day of the previous month of the given date.

The getLastDayPreviousMonth function takes a date object and returns the last day of the previous month of the given date.

If no date object is supplied to the methods, the current date is used.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev