How to use Date object In Javascript

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

How to Log the Current Date in JavaScript ?

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.

Table of Content

  • Using JavaScript Date object
  • Using JavaScript Date object with toString() Method
  • Using JavaScript Date object with toDateString() Method
  • Using JavaScript Date object with toLocaleDateString() Method
  • Using JavaScript Date object with toISOString() Method

Similar Reads

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....

Using JavaScript Date object with toString() Method

...

Using JavaScript Date object with toDateString() 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....

Using JavaScript Date object with toLocaleDateString() Method

...

Using JavaScript Date object with toISOString() 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....

Contact Us