WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

JavaScript Dates


The Date object gives you a chance to work with dates (years, months, days, hours, minutes, seconds, and milliseconds)


JavaScript Date Formats

A JavaScript date can be composed as a string:

or as a number:

Dates composed as numbers, indicates the quantity of milliseconds since January 1, 1970, 00:00:00.


Displaying Dates

In this instructional exercise we utilize a content to show dates inside a <p> component with id="demo":

Example

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

<script>
document.getElementById("demo").innerHTML = Date();
</script>
Try it Yourself »

The content above says: dole out the estimation of Date() to the substance (innerHTML) of the component with id="demo". 


Creating Date Objects

The Date object gives us a chance to work with dates.

A date comprises of a year, a month, multi day, 60 minutes, a moment, a second, and milliseconds.

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

There are 4 ways of starting a date:

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

Using new Date(), makes another date object with the current date and time:

Example

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

Using new Date(date string), makes another date object from the specified date and time:

Example

<script>
var d = new Date("October 18, 2018 11:13:00");
document.getElementById("demo").innerHTML = d;
</script>
Try it Yourself »

Using new Date(number), makes another date object as zero time in addition to the number.

Zero time is 01 January 1970 00:00:00 UTC. The number is determined in milliseconds:

Example

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

Using new Date(7 numbers), makes another date object with the specified date and time:

The 7 numbers indicate the year, month, day, hour, moment, second, and millisecond, in that order:

Example

<script>
var d = new Date(99,5,24,11,33,30,0);
document.getElementById("demo").innerHTML = d;
</script>
Try it Yourself »

Variants of the model above let us overlook any of the last 4 parameters:

Example

<script>
var d = new Date(99,5,24);
document.getElementById("demo").innerHTML = d;
</script>
Try it Yourself »


Date Methods

When a Date object is made, various methods enable you to work on it.

Date strategies enable you to get and set the year, month, day, hour, moment, second, and millisecond of articles, utilizing either neighborhood time or UTC (general, or GMT) time.


Displaying Dates

When you show a date object in HTML, it is consequently changed over to a string, with the toString() method.

Example

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

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

Is the equivalent as:

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

<script>
d = new Date();
document.getElementById("demo").innerHTML = d.toString();
</script>
Try it Yourself »

The toUTCString() strategy changes over a date to an UTC string (a date show standard).