top of page

The Date object lets you work with dates (years, months, days, hours, minutes, seconds, and milliseconds)

JavaScript Date Formats

A JavaScript date can be written as a string:

Mon Apr 25 2016 09:31:07 GMT+0530 (India Standard Time)

or as a number:

1461556867013

Dates written as numbers, specifies the number of milliseconds since January 1, 1970, 00:00:00.

Displaying Dates

In this tutorial we use a script to display dates inside a <p> element with id="demo":

 

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML = Date();
</script>

 

Creating Date Objects

The Date object lets us work with dates.

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

Date objects are created with the new Date() constructor.

There are 4 ways of initiating a date:

 

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

Using new Date(), creates a new date object with the current date and time:

 

<script>
var d = new Date();
document.getElementById("demo").innerHTML = d;
</script>

 

Date Methods

When a Date object is created, a number of methods allow you to operate on it.

Date methods allow you to get and set the year, month, day, hour, minute, second, and millisecond of objects, using either local time or UTC (universal, or GMT) time.

<!DOCTYPE html>
<html>
<body>

<p id="demo"></p>

<script>
var d = new Date();
document.getElementById("demo").innerHTML = d.toString();
</script>

</body>
</html>

 

 

 

 

 

 

 

Javascript Date Object

Work With Dates

bottom of page