Last updated: Mar 3, 2024
Reading timeยท7 min
To get the first and last Date of a month:
Date()
constructor to create a date object.1
for the day to get the first day of the
month.0
for the day to get the last day of the month.Here is a function that returns the first day of the month.
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
And here is a function that returns the last day of the month.
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
You can also use the following code sample if you only need to get the first and last day of the current month.
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():
function getFirstDayOfMonth(year, month) { return new Date(year, month, 1); }
The Date.getFullYear() method returns the year of the date.
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).
0
is January and 11
is December.We hardcoded 1
for the day to get the first day of the month.
Here is an example of how to get the first day of next month.
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);
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
to the value of the month because we want to get a value
representing the next month1
day by specifying 0
for the day
parameter.function getLastDayOfMonth(year, month) { return new Date(year, month + 1, 0); }
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.
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 tricky thing to remember is that months are zero-based and go from 0
(January) to 11
(December).
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.
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.
If you need to get the last day of next month, use the getFullYear()
and
getMonth()
methods.
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
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.
// ๐๏ธ 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
You can also use the Date()
constructor to get the first and last day of the
previous month.
// โ 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
We passed the following 3 arguments to the Date():
year
- a 4-digit integer value representing the year of the date.monthIndex
- an integer value from 0 (January) to 11 (December) that
represents the month.day
- an integer value representing the day of the month.To get the first day of the previous month, we had to subtract 1
from the
return value of the getMonth()
method.
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.
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.
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);
We got the first and last days of the previous month with a Date
object that
stores a date in January.
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.
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:
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.
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).
console.log(new Date().getMonth()); // ๐๏ธ 0 (January)
0
(January) to 11
(December).Define two reusable functions to not have to remember the intricacies of how dates work in JavaScript.
// โ 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 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.
You can learn more about the related topics by checking out the following tutorials: