How to get the current day, month, and year in JavaScript
In this short article, we’ll show you how to get the current day, month, and year in JavaScript. Here, we will use the pure vanilla JavaScript code to get the day, month year from the Date object in JavaScript.
Checkout more articles on JavaScript
Get the current Date object
Use the following code to get the current date object.
1 2 | const today = new Date(); // Wed May 18 2022 11:17:34 GMT+0530 (India Standard Time) |
Get current year
getFullYear()
– Provides current year like 2019.
1 2 | today.getFullYear(); // Output: 2022 |
Get current month
getMonth()
– Provides current month values 0-11. Where 0 for Jan and 11 for Dec.
1 2 | today.getMonth(); // Output: 4 |
Get current day
getDate()
– Provides day of the month values 1-31.
1 2 | today.getDate(); // Output: 18 |
Get current hour
getHours()
– Provides current hour between 0-23.
1 2 | today.getHours(); // Output: 11 |
Get current minutes
getMinutes()
– Provides current minutes between 0-59.
1 2 | today.getMinutes(); // Output: 17 |
Get current seconds
getSeconds()
– Provides current seconds between 0-59.
1 2 | today.getSeconds(); // Output: 34 |
Get current time
Let’s combine current hours, minutes and seconds to get the current time.
1 2 | today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds() // Output: 11:17:34 |
That’s it for today.
Thank you for reading. Happy Coding..!! 🙂