JS Tutorial
JS Version
JS Objects
JS Function
JS Classes
JS Async
JS HTML DOM
JS Browser BOM
JS Web API
JS AJAX
JS JSON
JS vs JQUERY
JS Graphics
JavaScript Date Formats
JavaScript Date Output
Independent of input format, JavaScript will (by default) output dates in full text string format:
Thu Jun 02 2022 13:03:44 GMT+0530 (India Standard Time)
JavaScript ISO Dates
ISO 8601 is the international standard for the representation of dates and times.
The ISO 8601 syntax (YYYY-MM-DD) is also the preferred JavaScript date format.
Complete date
Example
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript ISO Dates</h2>
<p id=”demo”></p>
<script>
const d = new Date(“2015-03-25”);
document.getElementById(“demo”).innerHTML = d;
</script>
</body>
</html>
Output
JavaScript ISO Dates
Wed Mar 25 2015 05:30:00 GMT+0530 (India Standard Time)ISO Dates (Year and Month)
ISO dates can be written without specifying the day (YYYY-MM):
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript ISO Dates</h2>
<p id=”demo”></p>
<script>
const d = new Date(“2015-03”);
document.getElementById(“demo”).innerHTML = d;
</script>
</body>
</html>
Output
JavaScript ISO Dates
Sun Mar 01 2015 05:30:00 GMT+0530 (India Standard Time)ISO Dates (Only Year)
ISO dates can be written without month and day (YYYY)
Example
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript ISO Dates</h2>
<p id=”demo”></p>
<script>
const d = new Date(“2015”);
document.getElementById(“demo”).innerHTML = d;
</script>
</body>
</html>
Output
JavaScript ISO Dates
Thu Jan 01 2015 05:30:00 GMT+0530 (India Standard Time)ISO Dates (Date-Time)
ISO dates can be written with added hours, minutes, and seconds (YYYY-MM-DDTHH:MM:SSZ):
Example
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript ISO Dates</h2>
<p>Separate date and time with a capital T.</p>
<p>Indicate UTC time with a capital Z.</p>
<p id=”demo”></p>
<script>
const d = new Date(“2015-03-25T12:00:00Z”);
document.getElementById(“demo”).innerHTML = d;
</script>
</body>
</html>
Output
JavaScript ISO Dates
Separate date and time with a capital T.
Indicate UTC time with a capital Z.
Wed Mar 25 2015 17:30:00 GMT+0530 (India Standard Time)Date and time is separated with a capital T.
UTC time is defined with a capital letter Z.
If you want to modify the time relative to UTC, remove the Z and add +HH:MM or -HH:MM instead:
Example
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript ISO Dates</h2>
<p>Modify the time relative to UTC by adding +HH:MM or subtraction -HH:MM to the time.</p>
<p id=”demo”></p>
<script>
document.getElementById(“demo”).innerHTML =
new Date(“2015-03-25T12:00:00-06:00”);
</script>
</body>
</html>
Output
JavaScript ISO Dates
Modify the time relative to UTC by adding +HH:MM or subtraction -HH:MM to the time.
Wed Mar 25 2015 23:30:00 GMT+0530 (India Standard Time)JavaScript Long Dates
Long dates are most often written with a “MMM DD YYYY” syntax like this:
Example
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript new Date()</h2>
<p id=”demo”></p>
<script>
const d = new Date(“Mar 25 2015”);
document.getElementById(“demo”).innerHTML = d;
</script>
</body>
</html>
Output
JavaScript new Date()
Wed Mar 25 2015 00:00:00 GMT+0530 (India Standard Time)JavaScript Short Dates
Short dates are written with an “MM/DD/YYYY” syntax like this:
Example
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript new Date()</h2>
<p id=”demo”></p>
<script>
const d = new Date(“03/25/2015”);
document.getElementById(“demo”).innerHTML = d;
</script>
</body>
</html>
Output