JavaScript Date Formats

JavaScript Date Output

JavaScript date output format will be a full format date string

ISO dates

ISO 8601 is international standard for date and time format

It is written as YYYY-MM-DD format

<!DOCTYPE html>
<html>
<body>

<span id="date"></span>
 
<script>
var d = new Date("2016-06-04");
document.getElementById('date').innerHTML=d;
</script>

</body>
</html>

Download the code Run the code

ISO 8601 dates can be written without day part. If day part is omitted than by default it will take 1

<!DOCTYPE html>
<html>
<body>

<span id="date"></span>
 
<script>
var d = new Date("2016-06");
document.getElementById('date').innerHTML=d;
</script>

</body>
</html>

Download the code Run the code

If month and day part will be omitted, it will take first month and day

<!DOCTYPE html>
<html>
<body>

<span id="date"></span>
 
<script>
var d = new Date("2016");
document.getElementById('date').innerHTML=d;
</script>

</body>
</html>

Download the code Run the code

Optional Time in HH:MM:SS format can be added to date

<!DOCTYPE html>
<html>
<body>

<span id="date"></span>
 
<script>
var d = new Date("2016-06-04T21:25:50"); 
document.getElementById('date').innerHTML=d;
</script>

</body>
</html>

Download the code Run the code

Long dates

JavaScript long dates can be written in MMM DD YYYY format

<!DOCTYPE html>
<html>
<body>

Month names are case insensitive and comma within date string will be ignored :
<p id="date"></p>
<p id="date1"></p>
<p id="date2"></p>
<p id="date3"></p>
<p id="date4"></p>
 
<script>
var d = new Date("Jun 04 2016"); 
var d1 = new Date("04 Jun 2016"); 
var d2 = new Date("June 04 2016"); 
var d3 = new Date("04 June 2016"); 
var d4 = new Date("JANUARY, 25, 2015"); 

document.getElementById('date').innerHTML=d;
document.getElementById('date1').innerHTML=d1;
document.getElementById('date2').innerHTML=d2;
document.getElementById('date3').innerHTML=d3;
document.getElementById('date4').innerHTML=d4;
</script>

</body>
</html>

Download the code Run the code

Short dates

Short dates are written in MM/DD/YYYY or YYYY/MM/DD format

<!DOCTYPE html>
<html>
<body>

Short dates can be written in MM/DD/YYYY or YYYY/MM/DD format.
<p id="date"></p>
<p id="date1"></p>
 
<script>
var d = new Date("06/04/2016"); 
var d1 = new Date("2016/06/04"); 

document.getElementById('date').innerHTML=d;
document.getElementById('date1').innerHTML=d1;

</script>

</body>
</html>

Download the code Run the code

Full Date format

JavaScript date object can take full date format as written by date object without any arguments

<!DOCTYPE html>
<html>
<body>

Javascript date object can take full date format
<p id="date"></p>

<script>
var ldate = new Date("Sun Jun 05 2016 00:00:00 GMT+0530 (India Standard Time)"); 

document.getElementById('date').innerHTML=ldate;

</script>

</body>
</html>

Download the code Run the code

Timezone

Below timezone are allowed in JavaScript Date object

Time Zone	Description
UTC		Coordinated Universal Time
GMT		Greenwich Mean Time
EDT		(US) Eastern Daylight Time
CDT		(US) Central Daylight Time
MDT		(US) Mountain Daylight Time
PDT		(US) Pacific Daylight Time
EST		(US) Eastern Standard Time
CST		(US) Central Standard Time
MST		(US) Mountain Standard Time
PST		(US) Pacific Standard Time

If timezone is not specified when get/set date object, than it will use browser’s default timezone.

Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .