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 datetime" title="DateTime">Date object in JavaScript.
Checkout more articles on JavaScript
- base64-image-in-a-new-tab-using-javascript" title="Open base64 Image in a new tab using JavaScript">Open base64 Image in a new tab using JavaScript
- file-input-using-javascript" title="How to clear an HTML file input using JavaScript">How to clear an HTML file input using JavaScript
- string-using-javascript" title="Capitalize the first letter of a string using JavaScript">Capitalize the first letter of a string using JavaScript
- How to get the original image size (width and height) using JavaScript
- math-functions-in-javascript" title="Math functions in JavaScript">Math functions in JavaScript
Get the current Date object
Use the following code to get the current date object.
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.
today.getFullYear();
// Output: 2022
Get current month
`getMonth()` – Provides current month values 0-11. Where 0 for Jan and 11 for Dec.
today.getMonth();
// Output: 4
Get current day
`getDate()` – Provides day of the month values 1-31.
today.getDate();
// Output: 18
Get current hour
`getHours()` – Provides current hour between 0-23.
today.getHours();
// Output: 11
Get current minutes
`getMinutes()` – Provides current minutes between 0-59.
today.getMinutes();
// Output: 17
Get current seconds
`getSeconds()` – Provides current seconds between 0-59.
today.getSeconds();
// Output: 34
Get current time
Let’s combine current hours, minutes and seconds to get the current time.
today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds()
// Output: 11:17:34
That’s it for today.
Thank you for reading. Happy Coding..!! 🙂