How to Get Current Date & Time in JavaScript
How do I get the current date and time in JavaScript? Also, how to get the date in Y-m-d format in JavaScript? Similar, how to get time in H:i:s format in JavaScript? This tutorial will help you to get the current date and time in JavaScript. Moreover, you can also get date and time in your custom formats like yyyy-mm-dd and hh:mm:ss formats.
There are various methods available to get the date in JavaScript. You can get date values like years, months, days, hours, minutes, seconds, milliseconds from a Date object.
Get Current Date & Time in JavaScript
JavaScript Date()
function to create an object with the current date and time in UTC (universal, or GMT) timezone.
var today = new Date();
By default above output will return dates in full-text string format. You can see below output.
Get Current Date in JavaScript
You can use getFullYear()
, getMonth()
, getDate()
method to get the current date in “Y-m-d” format using JavaScript.
var today = new Date();
var date = today.getFullYear() + '-' + (today.getMonth()+1) + '-' + today.getDate();
Output
- getFullYear() – This method is use to get the current year in YYYY format.
- getMonth() – This method is use to get the current month (0-11). However, starting from 0 as of January and 11 as of December. You need to add +1 to get the actual month.
- getDate() – This method is use to get the current date (1-31).
Get Current Time in JavaScript
You can use getHours()
, getMinutes()
, getSeconds()
method to get the current time in “H:i:s” format using JavaScript.
var today = new Date();
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
Output
- getHours() – It is used to get the current hour (0-23).
- getMinutes() – It is used to get the current minutes (0-59).
- getSeconds() – It is used to get the current seconds (0-59).
Current Date & Time Both in JavaScript
You can use the following script to get the current date and time in “Y-m-d H:i:s” format using JavaScript. Simply, you have to combine the above JavaScript code into one variable.
var today = new Date();
var date = today.getFullYear() + '-' + (today.getMonth()+1) + '-' + today.getDate();
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
var date_time = date + ' ' + time;
The output of the above date_time
variable is
Get Current Milliseconds in JavaScript
The getMilliseconds()
method is used to get the current milliseconds from a Date object.
var today = new Date();
var milliseconds = today.getMilliseconds();
Output
- getMilliseconds() – This method is used to fetch the current milliseconds (0-999).
JavaScript Date.now() Method
The Date.now()
method returns the number of milliseconds elapsed since January 1, 1970, 00:00:00 UTC. However, now() is a static method of Date. And it will always use as Date.now().
var milliseconds = Date.now();
The above variable returns the milliseconds since January 1, 1970, 00:00:00 UTC. You can see the following output.
Note: JavaScript uses milliseconds to measure the unit of time.
We hope you have found this article helpful. Let us know your questions or feedback if any through the comment section in below. You can subscribe our newsletter and get notified when we publish new WordPress articles for free. Moreover, you can explore here other JavaScript related articles.
If you like our article, please consider buying a coffee for us.
Thanks for your support!
Buy me a coffee!
Join the Discussion.