How to Get the Current Date in JavaScript

In JavaScript, we can get the current date by using the new Date() object.

  • It is an inbuilt data type that works with dates and times.

  • The Date object represents a date and time with millisecond precision within 100 million days before or after 1/1/1970.

  • We can create the Date object by using thenew keyword.

    • Syntax:
    
     
     const date = new Date();
     console.log(date);
    
    • Output:
    
     
    Wed Aug 17 2022 9:11:53 GMT+0530 (India Standard Time)
    
The date object reflects the current system time on the computer on which the browser is running. It is also dependent upon the browser. 
  • By default, it uses our browser's time zone and displays the date as a full-text string, such as "Wed Aug 17 2022 9:11:53 GMT+0530 (India Standard Time)" that contains the current day, date, time, and time zone.

Let's look at how we can get the current date out of this long string. We will improve its readability by utilizing some JavaScript methods that work with date objects.

How to Use JavaScript Date Methods

The date object supports numerous date methods. To get the current date, we will use the following methods :

  • getFullYear() The method returns the year of the specified date as a four-digit number (YYYY).

    Syntax:

    
     
     let year=dateObj.getFullYear(); 
    

    Example:

    
     
    const date = new Date(); 
    
    let year= date.getFullYear(); 
    
    console.log(year); 
    

    OUTPUT:

    
     
    2022
    
  • getMonth() Using the getMonth() method, we can get the month number from a given date object (0-11). It has a zero-based index.

    Syntax:

    
     
     let month=dateObj.getMonth(); 
    

    Example:

    
     
    const date = new Date(); 
    
    //Adding 1 because the month index starts from 0 
    
    let month= date.getMonth()+1; 
    
    console.log(month); 
    

    OUTPUT:

    
     
    8
    
    • If we want to show the month in the format of MM, i.e., 08. The built-in date class doesn't offer formatting features, unlike other libraries. We need to come up with a solution. We can use Javascript's methods on strings like padStart(length, substring). The pad puts before our string with the substring before it until it reaches a certain length. To put 0 before 8, we will pad our string with "0" until it reaches length 2. Since we are using the String methods, we need to typecast the month type number to a String.

      
       
        let month = String(date.getMonth()+1).padStart(2,"0");
        
        //  This will change our month format to MM,i.e.,08 
      

      We can use the same method for getDate() also.

  • getDate() It fetches the date of a month from a given Date object. It returns the day as a number (1-31).

    Syntax:

    
     
     let day=dateObj.getDate(); 
    

    Example:

    
     
    const date = new Date(); 
    
    let day= date.getDate(); 
    
    console.log(day); 
    

    OUTPUT:

    
     
    16
    

NOTE : To get the current day we will use the getDate() method. The getDay() method returns the day of the week, not the current day number.

Now, let's combine all of these methods to get current date in JavaScript.


 
// Date object
const date = new Date();

let currentDay= String(date.getDate()).padStart(2, '0');

let currentMonth = String(date.getMonth()+1).padStart(2,"0");

let currentYear = date.getFullYear();

// we will display the date as DD-MM-YYYY 

let currentDate = `${currentDay}-${currentMonth}-${currentYear}`;

console.log("The current date is " + currentDate); 

 

Output:


 
The current date is 17-08-2022

 

Let's look at some other methods to get current date in javascript.