How to Get Current Date Time in JavaScript

Harshit Jindal Feb 02, 2024
  1. Use the getDate(), getMonth() and getYear() Methods to Get Current Date in JavaScript
  2. Use the Date.toDateString() Method to Get Current Date in JavaScript
  3. Use the Date.toISOString() Method to Get Current Date in JavaScript
  4. Use Inbuilt Method toLocaleDateString() to Get Current Date in JavaScript
How to Get Current Date Time in JavaScript

In the modern web, almost every website we use is built using JavaScript. The date and time of a user’s visit are crucial to maintaining a record of their browsing pattern. JavaScript provides us with a Date object that can be used to get the current date and time. We can then extract the date and time from it in any desired form we like. In this tutorial, we will see different ways to get the current date in JavaScript according to our requirements.

Use the getDate(), getMonth() and getYear() Methods to Get Current Date in JavaScript

In this approach, we get different parts of a date using their respective methods. The date is extracted using getDate(), the month is extracted using getMonth() and the year is extracted using the getYear() method. The month extracted is 0 indexed, so we need to add 1 to get the current month. The single-digit day and month can be padded with 0 to make them both 2 digits. We then concatenate results from all 3 parts to obtain the final date.

let date = new Date();
let output = String(date.getDate()).padStart(2, '0') + '/' +
    String(date.getMonth() + 1).padStart(2, '0') + '/' + date.getFullYear();
console.log(output);

Output:

"25/06/2021"

Use the Date.toDateString() Method to Get Current Date in JavaScript

The toDateString() method returns the day and date part of a Date object. The return type is a string that consists of 3 letters specifying day, 3 letters specifying month, followed by day and date in numeric format. This method is very convenient compared to the above method, where we have to use a separate function to get the date, month, and year. But if we want the date in the numeric format, then this method is not of much help.

let date = new Date().toDateString();
console.log(date);

Output:

"Mon Jun 14 2021"

Use the Date.toISOString() Method to Get Current Date in JavaScript

This method is used to return the date and time in ISO 8601 format. It usually returns the output in 24 characters long format such as YYYY-MM-DDTHH:mm:ss.sssZ. We can notice that the date and time are separated from each other by the letter T. So, if we split the ISO Date/Time String by T, we get two strings, one containing the date and the other containing the time. This way, we can get the current date from the ISO string. The one thing untypical is that the output is separated by - instead of /. So, one should use a different method as per requirement.

let date = new Date();
console.log(date.toISOString().split('T')[0]);

Output:

"2021-06-14"

Use Inbuilt Method toLocaleDateString() to Get Current Date in JavaScript

This function is intended to get the language-sensitive date portion of the Date object. The locale argument specifies the language tag and the country, and options help to customize it further. But we can also use it to get the current date from the Date object simply.

let date = new Date();
console.log(date.toLocaleDateString());

Output:

"6/25/2021"
Harshit Jindal avatar Harshit Jindal avatar

Harshit Jindal has done his Bachelors in Computer Science Engineering(2021) from DTU. He has always been a problem solver and now turned that into his profession. Currently working at M365 Cloud Security team(Torus) on Cloud Security Services and Datacenter Buildout Automation.

LinkedIn

Related Article - JavaScript DateTime