Javascript Dates

This object can be used to get year, month and day. We can display a timer on the webpage by the help of JavaScript date object.

We can use different Date constructors to create date object. It provides methods to get and set day, month, year, hour, minute and seconds.

Date objects are created with the new Date() constructor. There are 4 ways to create a new date object:


Date() method

This method creates a new date object with the current date and time.

Input:-
 <script>  
    var d = new Date();   
    document.writeln(d); 
</script>

Output:-


Date(milliseconds)

This method creates a new date object as zero time plus milliseconds.

Input:-
 <script>  
    var d = new Date(100000000000);
    document.writeln(d);
</script>

Output:-


Date(dateString)

This method creates a new date object from a date string.

Input:-
 <script>  
    var d = new Date("October 13, 2014 11:13:00");
    document.writeln(d);
</script>

Output:-


Date(year, month, day, hours, minutes, seconds, milliseconds)

This method creates a new date object with a specified date and time.

Note:- JavaScript counts months from 0 to 11. January is 0. December is 11.

Input:-
 <script>  
    var d = new Date(2018, 11, 24, 10, 33, 30);
    document.writeln(d);
</script>

Output:-