Date Object

The Date Object

In JavaScript, the Date object is used to create dates and times.

A date consists of a year, a month, a day, an hour, a minute, a second, and milliseconds.

The Date objects are usually created by using new Date () constructor, in order to show the current time and date.

 

The four ways which are used to create a new date object are as follows:

  • new Date ();
  • new Date (milliseconds);
  • new Date (year, month, day, hours, minutes, seconds, milliseconds);
  • new Date (date string);

 

Code:

<html>
<body>
<h2>JavaScript Date Object</h2>
<script>
var x = new Date();
document.write("Current Date and Time is "+x);
</script>
</body>
</html>

 

 

Output: