Last updated: Mar 3, 2024
Reading timeยท5 min
To get the number of days in a month:
new Date()
constructor, passing it 0
for the days.getDate()
method on the result to get the number of days in the
month.function getDaysInMonth(year, month) { return new Date(year, month, 0).getDate(); } const date = new Date(); const currentYear = date.getFullYear(); const currentMonth = date.getMonth() + 1; // ๐๏ธ months are 0-based // ๐๏ธ Current Month const daysInCurrentMonth = getDaysInMonth( currentYear, currentMonth, ); console.log(daysInCurrentMonth); // ๐๏ธ 31 // ๐๏ธ Other Months const daysInJanuary = getDaysInMonth(2025, 1); console.log(daysInJanuary); // ๐๏ธ 31 const daysInSeptember = getDaysInMonth(2025, 9); console.log(daysInSeptember); // ๐๏ธ 30
We passed the following 3 arguments to the new Date() constructor:
Date
objectDate
object. Month
indexes are zero-based where 0
is January and 11
is December.Date
object. When 0
is used for the days,
we get back the last day of the previous month. We use this approach to
balance out the zero-based month index.getMonth()
to the function, make sure to add 1
to the month index.const date = new Date(); // ๐๏ธ add 1 to get correct value const currentMonth = date.getMonth() + 1;
Specifying 0
for the days
parameter, allows us to use intuitive month
indexes when using the new Date()
constructor.
For example, passing a month index of 2
, gives us the last day in February,
and not March.
console.log(new Date(2025, 1, 0)); // ๐๏ธ Fri January 31 2025 console.log(new Date(2025, 2, 0)); // ๐๏ธ Fri Feb 28 2025
The last step is to call the Date.getDate() method.
console.log(new Date(2025, 1, 0).getDate()); // ๐๏ธ 31 console.log(new Date(2025, 2, 0).getDate()); // ๐๏ธ 28
The method returns an integer from 1
to 31
, which represents the day of the
month for a given date according to local time.
Getting the integer representation of the last day of the month is the equivalent of getting the number of days in the month.
To get the number of days in the current month:
new Date()
constructor to get a date object that corresponds to the
last day of the current month.getDate()
method to get an integer representing the last day of
the month.function getDaysInCurrentMonth() { const date = new Date(); return new Date( date.getFullYear(), date.getMonth() + 1, 0, ).getDate(); } const result = getDaysInCurrentMonth(); console.log(result); // ๐๏ธ 31
We passed the following 3 arguments to the new Date() constructor:
0
is January
and 11
is December. This is why we added 1
to the result.0
is used as the day, we
get back the last day of the previous month. We use this approach to
balance out the zero-based month index.It's quite confusing but the getMonth()
method returns a zero-based index.
const date = new Date('January 04, 2025 05:24:07'); console.log(date.getMonth()); // ๐๏ธ 0
To balance this out, we passed 0
to the days
parameter of the new Date()
constructor to get the last day of the prior month.
For example, passing a month index of 2
, gives us the last day of February and
not March.
console.log(new Date(2025, 1, 0)); // ๐๏ธ Fri January 31 2025 console.log(new Date(2025, 2, 0)); // ๐๏ธ Fri Feb 28 2025
new Date()
constructor returns an object representing the last day of the current month.The last step is to call the Date.getDate() method.
function getDaysInCurrentMonth() { const date = new Date(); return new Date(date.getFullYear(), date.getMonth() + 1, 0).getDate(); } const result = getDaysInCurrentMonth(); console.log(result); // ๐๏ธ 31
The method returns an integer (from 1
to 31
) that represents the day of the
month for a given date.
Getting the integer representation of the last day of the current month is the equivalent of getting the number of days in the month.
To get the day of the year:
function getDayOfYear(date = new Date()) { const timestamp1 = Date.UTC( date.getFullYear(), date.getMonth(), date.getDate(), ); const timestamp2 = Date.UTC(date.getFullYear(), 0, 0); const differenceInMilliseconds = timestamp1 - timestamp2; const differenceInDays = differenceInMilliseconds / 1000 / 60 / 60 / 24; return differenceInDays; } console.log(getDayOfYear(new Date('2022-02-01'))); // ๐๏ธ 32 console.log(getDayOfYear(new Date('2022-01-29'))); // ๐๏ธ 29 console.log(getDayOfYear(new Date('2022-03-01'))); // ๐๏ธ 60 console.log(getDayOfYear(new Date('2022-12-31'))); // ๐๏ธ 365
We created a reusable function that returns the number of the day of the year.
The function takes a Date
object as a parameter or uses the current date if
none is provided
The
Date.UTC()
method takes similar parameters to the Date()
constructor, but treats them as
UTC and returns the number of milliseconds since the 1st of January 1970,
00:00:00 UTC.
Date.UTC
method to avoid any Daylight Saving Time (DST) issues.In the first call to the method, we got a timestamp of the passed-in date.
We used the following 3 date-related methods:
Date.getFullYear() method - returns a four-digit number representing the year that corresponds to a date.
Date.getMonth() - returns an
integer between 0
(January) and 11
(December) and represents the month for
a given date. Yes, unfortunately, the getMonth
method is off by 1
.
Date.getDate() - returns an
integer between 1
and 31
representing the day of the month for a specific
date.
In our second call to the Date.UTC()
method, we got a timestamp for the last
day of the previous year.
function getDayOfYear(date = new Date()) { const timestamp1 = Date.UTC( date.getFullYear(), date.getMonth(), date.getDate(), ); const timestamp2 = Date.UTC(date.getFullYear(), 0, 0); const differenceInMilliseconds = timestamp1 - timestamp2; const differenceInDays = differenceInMilliseconds / 1000 / 60 / 60 / 24; return differenceInDays; }
We passed 0
for the month of January and 0
for the day.
Note that the value for the day of the month is not zero-based, it is one-based.
Date
object in JavaScript automatically takes care of rolling back or rolling over the values for the month and year, so we got a timestamp for the last day of the previous year.Subtracting the second timestamp from the first gives us the difference between the passed-in date and the last day of the previous year in milliseconds.
The last step is to convert the difference from milliseconds to days to get the day of the year.
You can learn more about the related topics by checking out the following tutorials: