Open In App

How to Log the Current Date in JavaScript ?

Last Updated : 23 Jan, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The Date object is an inbuilt object constructor of JavaScript language. It is used to work with dates and times. The Date object is created by using the new keyword, i.e. new Date(). The current date can be logged on the console in JavaScript using the below-discussed methods.

Using JavaScript Date object

The Date() constructor instantiates a Date object which sets the current date and time depending on the browser’s time zone. This also comes with a wide variety of methods.

Example: The below code uses the Date object with its corresponding methods to log the current date in JavaScript.

JavaScript
// Create a new Date object
const currentDate = new Date();

const day = currentDate.getDate(), 
    // Since month starts from 0 (Jan)
    month = currentDate.getMonth() + 1,
    year = currentDate.getFullYear(); 
    
console.log(`${day}/${month}/${year}`);  

Output
23/1/2024

Using JavaScript Date object with toString() Method

We can convert the current date to a string using the toString() method and then split it into an array of words using the space delimiter. Thereafter, the splice method is used on the array to keep the words from indices 1 to 3 i.e. the day, month and the year. Finally, the join method is used to join the resultant array.

Example: The below code uses the Date object with toString() method and its associated methods to log the current date in JavaScript.

JavaScript
// Create a new Date object
const currentDate = new Date();
console.log("Before Split: ", currentDate.toString());
console.log("After Split: ", currentDate.toString().
split(' ').splice(1, 3).join(' '));

Output
Before Split:  Tue Jan 23 2024 09:17:49 GMT+0000 (Coordinated Universal Time)
After Split:  Jan 23 2024

Using JavaScript Date object with toDateString() Method

The date object comes with a method known as toDateString() which simply returns the date portion of this date interpreted in the local timezone.

Example: The below code uses the Date object with toDateString() method to log the current date in JavaScript.

JavaScript
// Create a new Date object
const currentDate = new Date();

console.log(currentDate.toDateString());

Output
Tue Jan 23 2024

Using JavaScript Date object with toLocaleDateString() Method

The date object comes with a method known as toLocaleDateString() with parameters such as a locale which represents the location, of which the date format should be shown, and a list of options to indicate the format of how the year, month and the day of the date should be represented.

Example 1: The below code uses the Date object with toLocaleDateString() method with a specified locale (en-GB) and a list of options to represent the format of the year, the month and the day to log the current date in JavaScript.

JavaScript
const currentDate = new Date().toLocaleDateString("en-GB", {
    "year": "numeric",
    "month": "numeric",
    "day": "numeric"
});

console.log(currentDate);

Output
23/01/2024

Example 2: The below code uses the Date object with toLocaleDateString() method with a specified locale (en-IN) and a list of options to represent the format of the year, the month and the day to log the current date in JavaScript.

JavaScript
const currentDate = new Date().toLocaleDateString("en-IN", {
    "year": "numeric",
    "month": "short",
    "day": "numeric"
});

console.log(currentDate);

Output
23 Jan 2024

Using JavaScript Date object with toISOString() Method

The date object comes with a method known as toISOString() which simply returns the date in the date time string format, a format which is based on ISO 8601.

Example: The below code uses the Date object with toISOString() method to log the current date in JavaScript.

JavaScript
console.log(new Date().toISOString().split("T")[0]);

Output
2024-01-23

Next Article

Similar Reads