JavaScript Date Methods

In this tutorial, we shall learn the JavaScript Date Methods available. Following is a quick list.

Get Methods

  • getDate() returns Date as String Object.
  • getDay() returns Day of the month as Number (1 to 31).
  • getFullYear() returns current year in Full Year format as Number (YYYY – four digit year).
  • getHours() returns number of hours passed for the day in date (0 to 24).
  • getMilliseconds() returns the number of milliseconds passed in the date (0 to 999).
  • getMinutes() returns the number of minutes passed in the date (0 to 59).
  • getMonth() returns the number of month as Number (0 to 11).
  • getSeconds() returns the number of seconds passed in the date as Number (0 to 59).
  • getTime() returns the number of milliseconds passed from (00:00 01-01-1970) till the date as Number.

Set Methods

  • setDate() sets the argument as the Date.
  • setDay() sets the argument (1 to 31) as the day of the month in Date Object.
  • setFullYear() sets the argument (YYYY- four digit) as the year in Date Object.
  • setHours() sets the argument (0 ot 59) as the hours in the Date Object.
  • setMilliseconds() sets the argument (0 to 999) as the number of milliseconds in the Date Object.
  • setMinutes() sets the argument (0 to 59) as the number of minutes in the Date Object.
  • setMonth() sets the argument (0 to 11) as the month in the Date Object.
  • setSeconds() sets the argument (0 to 59) as the number of seconds in the Date Object.
  • setTime() sets the argument (number of milliseconds from 00:00 01-01-1970) as the value of Date Object.

Example – Date get methods

The following example demonstrates the Date() get methods.

index.html

</>
Copy
<!doctype html>
<html>
<body>
    <h1>JavaScript Date Methods Example</h1>
    <p id="message"></p>
    
    <script>
        <!-- your JavaScript goes here -->
        var date = new Date();
        
        var msg = "";
        
        // Date
        msg += "Date : ";
        msg += date; msg +="<br><hr>";
        
        msg += "getDate() : ";
        msg += date.getDate(); msg +="<br>";
        
        msg += "getDay() : ";
        msg += date.getDay(); msg +="<br>";
        
        msg += "getFullYear() : ";
        msg += date.getFullYear(); msg +="<br>";
        
        msg += "getHours() : ";
        msg += date.getHours(); msg +="<br>";
        
        msg += "getMilliseconds() : ";
        msg += date.getMilliseconds(); msg +="<br>";
        
        msg += "getMinutes() : ";
        msg += date.getMinutes(); msg +="<br>";
        
        msg += "getMonth() : ";
        msg += date.getMonth(); msg +="<br>";
        
        msg += "getSeconds() : ";
        msg += date.getSeconds(); msg +="<br>";
        
        msg += "getTime() : ";
        msg += date.getTime(); msg +="<br>";
        
        document.getElementById("message").innerHTML = msg;
    </script>
    
</body>
</html>

Example – Date set methods

The following example demonstrates the Date() set methods.

index.html

</>
Copy
<!doctype html>
<html>
<body>
    <h1>JavaScript Date Methods Example</h1>
    <p id="message"></p>
    
    <script>
        <!-- your JavaScript goes here -->
        var msg = "";
        
        var date = new Date();
        
        msg += "Date before set methods : ";
        msg += date;
        msg += "<br>";
        
        date.setDate(24);
        date.setFullYear(2020);
        date.setHours(14);
        date.setMilliseconds(556);
        date.setMinutes(41);
        date.setMonth(8);
        date.setSeconds(22);
        
        msg += "Date after set methods : ";
        msg += date;
        msg += "<br>";
        
        date.setTime(1516993680832);
        
        msg += "Date after setTime() : ";
        msg += date;
        msg += "<br>";
        
        document.getElementById("message").innerHTML = msg;
    </script>
	
</body>
</html>

Conclusion

In this JavaScript Tutorial, we learned about different methods of Date Object, with examples.