JavaScript Date
In JavaScript Date Object is data type build into the JavaScript language. Data object are created with the
new Date(). JavaScript Date instance that represents a single moment in time. JavaScript date object can only be instantiated by calling JavaScript as a constructor.
Syntax
We can use following syntaxes to create a Date object using Date() constructor.
new Date();
new Date(value);
new Date(dateString);
new Date(year, month, day, hours, minutes, seconds, milliseconds);
NOTE: Parameters in bracket is always optional.
Here descriptions of parameter:
value
It representing the number of milliseconds since 1 January 2017 00:00:00 UTC, with leap seconds ignored.
dataString
String value representing a date. The string should be in a format recognized by the
Date.parse()method.
year
Integer value representing the year. Value from 0 to 99 map of the years 2000 to 2099.
month
Integer value representing the month, beginning with 0 for January to 11 for December.
date
Optional. Integer value representing the day of the month.
hours
Optional. Integer value representing the hours of the day.
minutes
Optional. Integer value representing the minute segment of a time.
seconds
Optional. Integer value representing the second segment of a time.
milliseconds
Optional. Integer value representing the millisecond segment of a time.
Exmple of Date
<!DOCTYPE html>
<html>
<body>
<script>
var date=new Date();
var day=date.getDate();
var month=date.getMonth()+1;
var year=date.getFullYear();
document.write("<br>Date: "+day+"/"+month+"/"+year);
</script>
</body>
</html>
Try Now
Output
Example of current time
<!DOCTYPE html>
<html>
<body>
Current Time: <span id="txt"></span>
<script>
var today=new Date();
var h=today.getHours();
var m=today.getMinutes();
var s=today.getSeconds();
document.getElementById('txt').innerHTML=h+":"+m+":"+s;
</script>
</body>
</html>
Try Now
Output