How to Get current Date & Time in JavaScript
π
July 10, 2021
π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.
- Validate the value by regular expression in JavaScript
- How to detect the user browser in JavaScript
- Sorting an array in JavaScript
- JavaScript switch statement with example
- ES6 Rest Parameter in JavaScript
var today = new Date();
// Output: Sun Dec 22 2019 13:18:01 GMT+0530 (India Standard Time)
- 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.
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.
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
I hope you find this article helpful.
Thank you for reading. Happy Coding..!!