Date formats are categorized into 4 types.
Date Initiations
- ISO Dates
- Short Dates
- Long Dates
- Full Format
All these are input formats. But, the output format of dates will be represented in string format by default.
The international standard to represent dates and time is ISO 8601(YYYY-MM-DD).
[html]
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<p id="test"></p>
<p id="test1"></p>
<p id="test2"></p>
<script>
//complete date
document.getElementById("demo").innerHTML = new Date("2015-03-25");
//only year and month
document.getElementById("test").innerHTML = new Date("2015-03");
//Only year
document.getElementById("test1").innerHTML = new Date("2015");
//date with hour,minutes and seconds
document.getElementById("test2").innerHTML = new Date("2015-03-25T12:00:00");
</script>
</body>
</html>[/html]
Output:
The syntax of short date format "MM/DD/YYYY" or "YYYY/MM/DD" is as follows.
[html]
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
//date as MM/DD/YYYY
document.getElementById("demo").innerHTML = new Date("03/25/2015");
</script>
</body>
</html>
[/html]
Output:
The syntax of Long date format "MMM DD YYYY" is as follows.
[html]
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = new Date("Mar 25 2015");
</script>
</body>
</html>
[/html]
Output:
Date Strings are allowed in full date format. If any errors occur in day name and time, JavaScript will ignore those errors.
[html]
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
new Date("Wed Mar 25 2015 09:56:24 GMT+0100 (W. Europe Standard Time)");
</script>
</body>
</html>
[/html]
Output: