Date in JavaScript

In JavaScript date() method is used show date which may be year, month and day. Timer can also be displayed using JavaScript date object.
Different syntax to create a date method are explained below.

Date constructor

You can use 4 variant of Date constructor to create date object.

1-- Date()
2-- Date(milliseconds)
3-- Date(dateString)
4-- Date(year, month, day, hours, minutes, seconds, milliseconds)


Example Date()

In this example new Date() is used to create date object to show current date and time according to the current browser's time zone.

<script type = "text/javascript">
var d = new Date();  
document.write(d); 
</script>                  
        

Output of above code


Example Date(milliseconds)

In this example new Date(milliseconds) is used to creates creates a new date object as zero time plus milliseconds:

<script type = "text/javascript">
var dm = new Date(0);  
document.write(dm); 
</script>                  
        

Output of above code


Example Date(dateString)

In this example new Date(dateString) is used to creates a new date object from a date string:

<script type = "text/javascript">
var ds = new Date("October 13, 2014 11:13:00");
document.write(ds); 
</script>                  
        

Output of above code


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

In this example new Date(year, month, ...) is used to creates a new date object with a specified date and time.
7 numbers specify year, month, day, hour, minute, second, and millisecond (in that order):

<script type = "text/javascript">
var dym = new Date(2018, 11, 24, 10, 33, 30, 0);
document.write(dym); 
</script>                  
        

Output of above code


Note:

Keep in mind: by default, JavaScript will use the browser's time zone and display a date as a full text string.








The Best

Comment here

If you have any query, if you want to know something about any of technical course related to computer science field, if you have any suggestion about relevant to uploaded content or if you anything wrong here (any mistake in content) than please contact us. Keep in mind, comment should be according to community guidelines.