To get the current year, month and day, we have to first create the current date object using the Date()
constructor and then we can call the getFullYear()
, getMonth()
and the getDate()
methods on this date object.
The Date()
is a built-in function in JavaScript. When used as a constructor i.e. new Date()
, it returns an object containing the current year, month, day, hours, minutes, seconds, etc.
It supports various methods that we can call on the date object. We will use the following three methods:
Date.getFullYear()
– It returns a four digits number representing the year specified by the date object. eg. 2000, 2021.Date.getMonth()
– It returns an integer between 0 and 11(inclusive) that represents the month specified by the date object. Integer 0(zero) represents the first month i.e. January, 1 represents the second month i.e. February and so on up to 11(December).Date.getDate()
– It returns an integer between 1 and 31(inclusive) representing the day of the month.
The following example uses the above methods to get the current year, month and day:
// Create the date object let date = new Date(); // Get year, month and day let year = date.getFullYear(); let month = date.getMonth() + 1; let day = date.getDate(); console.log(year); // 2022 console.log(month); // 11 console.log(day); // 18
Notice that the getMonth()
method is 0-based i.e. the month index starts at 0, therefore, we have explicitly added +1
to the month to get the exact month number.
One more thing you have to note here is that the Date()
can be used as a constructor as well as a function.
When we use the Date()
with the new keyword i.e. new Date()
, it acts as a constructor and returns a date object. Therefore, we can call any date method such as getMonth()
, getDate()
, etc. on this returned date object.
But, when we use the Date()
as a normal function, i.e. without the new keyword, it returns a date string, not a date object. So, if we call any date method on this date string, we will get a TypeError
.
// Date() used as a constructor let dateObj = new Date(); console.log(typeof dateObj); // object console.log(dateObj.getMonth()); // 10 // Date() used as a function let dateStr = Date(); console.log(typeof dateStr); // string console.log(dateStr.getMonth()); // --Error--
Get the Current Day Name
To get the current day name, we can use the Date.getDay()
method. The getDay()
method returns an integer from 0 to 6 representing the day of the week for the specified date. Here, integer 0 represents Sunday, 1 represents Monday and so on till Saturday(6).
// Create the date object let date = new Date(); let day = date.getDay(); console.log(day); // 0
If you want the exact day name instead of a raw integer, you can either create an array of week days starting from Sunday i.e. index = 0. OR use the built-in method date.toLocaleDateString()
.
1. Get the Week Day Name using the Week Days Array:
Create an array of the week days(starting at Sunday) and map the number returned from the getDay()
method to the days array.
Something like this:
// Week days array const days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; // Create the date object let date = new Date(); let dayIndex = date.getDay(); let dayName = days[dayIndex]; console.log(dayName); // Sunday
2. Get the Week Day Name using Date.toLocaleDateString() Method
This is a straightforward way to get the week day name from the specified date. The toLocaleDateString()
method returns a string representation of the date based on the locale passed to it.
It also provides four formating parameters weekday, year, month and day to get the shorter or longer version of the day or month name.
// Create the date object let date = new Date(); console.log(date.toLocaleDateString('en-US', {weekday: 'long'})); // Output: Sunday console.log(date.toLocaleDateString('en-US', {weekday: 'short'})); // Output: Sun
Get the Current Month Name
Just like the day name, we can use the two methods to get the current month name.
The first method, create an arary of 12 months starting at January and map the index returned by the getMonth()
method with the months array.
Something like this:
// Months Name Array const months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']; // Get current date object const date = new Date(); let monthIndex = date.getMonth(); let monthName = months[monthIndex]; console.log(monthName); // November
The second approach is much straightforward, use the toLocaleDateString()
method to directly extract the month name.
See the following example:
// Get current date object const date = new Date(); console.log(date.toLocaleDateString('en-US', {month: 'long'})); // Output: November console.log(date.toLocaleDateString('en-US', {month: 'short'})); // Output: Nov
Conclusion
In this article, we have learned how to get the current year, month and day. We did it using four built-in JavaScript methods getFullYear()
, getMonth()
, getDate()
and getDay()
.
We then learned how we can get the current month and week day name using the two approaches. First, using the month or days array and second using the toLocaleDateString()
method.