Get Current Date & Time in JavaScript
Today we will show you how to get current date & time in JavaScript. We will also show you how to get a current year, month, date, hours, minutes, seconds, etc.
Get Current Date & Time in JavaScript, display current date and time in html using javascript, javascript current date format, javascript get current date, javascript get current time, javascript get current timestamp, javascript new date from string, javascript new date format, display current date and time in html using html, JavaScript Date Objects, Get the current date, DateTime Manipulation.
Checkout more articles on JavaScript
Let’s get a current date and time by running the following command.
1 2 | var today = new Date(); // Output: Sun Dec 22 2019 13:18:01 GMT+0530 (India Standard Time) |
We have listed the methods which will provide us the information from the date.
- getFullYear() – Provides current year like 2019.
- getMonth() – Provides current month values 0-11. Where 0 for Jan and 11 for Dec.
- getDate() – Provides day of the month values 1-31.
- getHours() – Provides current hour between 0-23.
- getMinutes() – Provides current minutes between 0-59.
- getSeconds() – Provides current seconds between 0-59.
Refer following code to get an appropriate output.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | var today = new Date(); // Output: Sun Dec 22 2019 13:18:01 GMT+0530 (India Standard Time) today.getFullYear(); // Output: 2019 today.getMonth(); // Output: 11 today.getDate(); // Output: 22 today.getDay(); // Output: 0 today.getHours(); // Output: 13 today.getMinutes(); // Output: 18 today.getSeconds(); // Output: 1 today.getMilliseconds(); // Output: 114 today.getTime(); // Output: 1577000881114 (for GMT) |
Using the below script we can also get the current date and time in Y-m-d H:i:s
format.
1 2 3 4 5 6 | var today = new Date(); // Sun Dec 22 2019 13:18:01 GMT+0530 (India Standard Time) var date = today.getFullYear() + '-' + (today.getMonth() + 1) + '-' + today.getDate(); var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds(); var dateTime = date + ' ' + time; // Output: 2019-12-22 13:18:1 |
That’s it for today.
Thank you for reading. Happy Coding!