Explain the Date() object in JavaScript?

JavascriptObject Oriented ProgrammingProgramming

Date()

Date() method gives the current date and moreover using the Date() method we can get the date in a specified  time.format.

Live Demo

Example

<html>
<body>
<script>
var d = new Date();
document.write(d);
</script>
</body>
</html>

Output

Thu May 16 2019 05:29:15 GMT+0530 (India Standard Time)

using new Date() and 7 numbers inside it we can get the date even with milli seconds.

Live Demo

Example

<html>
<body>
<script>
var d = new Date(2019, 9, 19, 5, 33, 30, 0);
document.write(d);
</script>
</body>
</html>

Output

Sat Oct 19 2019 05:33:30 GMT+0530 (India Standard Time)

In the above example inside the date we have given in single line the year, month, day,hour,minute,second,millisecond and so on.

We also can get the time only in our specified format.For instance time with only year, month and day is give by

Live Demo

Example

<html>
<body>
<script>
var d = new Date(2019, 9, 19);
document.write(d);
</script>
</body>
</html>

Output

Sat Oct 19 2019 00:00:00 GMT+0530 (India Standard Time)
Published on 21-May-2019 13:35:35
Advertisements