Sun Oct 24 2021·1 min read
Photo by Persnickety Prints
new Date() constructor to
get a date object and call the
getFullYear() method on the result, e.g.
new Date().getFullYear(). The
getFullYear method will return a number
corresponding to the current year.
// 👇️ Get current Year const currentYear = new Date().getFullYear(); console.log(currentYear); // 👇️ Get year of specific date const date = new Date('January 04, 2025 05:24:07'); const yearOfDate = date.getFullYear(); // 👉️ 2025
We used the
to get a
Date object, on which we can call various methods.
For our use case, we called the Date.getFullYear method, which returns a number representing the year that corresponds to the current date.
Other commonly used methods on the
Date object are:
11(December) and represents the month for a given date. Yes, unfortunately the
getMonthmethod is off by
These methods allow you get the year / month / day for any date object, it doesn't have to be the current year.
All you have to do is pass the specific date into the
Date constructor on
which you call the methods.
const date = new Date('December 24, 2025 05:24:07'); const yearOfDate = date.getFullYear(); // 👉️ 2025 const monthOfDate = date.getMonth() + 1; // 12 const dayOfMonth = date.getDate(); // 24