JavaScript Date Format - How do I format a date in JavaScript



JavaScript Date Format

  • The JavaScript date object can be used to get a year, month and day. We can display a timer on the webpage with the help of a JavaScript date object.
  • There are many types of date formats in JavaScript: ISO Date, Short Date, and Long Date. The formats of JavaScript's date are defined as follows.
  • ISO date
    • "2020-08-01" (The International Standard)
  • Short date
    • "01/08/2020"
  • Long date
    • "Aug 01 2020" or "01 Aug 2020"

JavaScript ISO date

  • The ISO 8601 is the international standard for the times and dates, and the syntax (YYYY-MM-DD) of this standard is the preferred date format in JavaScript.
javascript-iso-date

Sample Code

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div>
<h1> Welcome to the Kaashiv infotech </h1>
<h3> It is an example of JavaScript's ISO date </h3>
<p id="para"> </p>
</div>
<script>
let val = new Date("2020-08-01");
document.getElementById("para").innerHTML = val;
</script>
</body>
</html>

Output

Welcome to Wikitechy
It is an example of JavaScript's ISO date
Sat Aug 01 2020 05:30:00 GMT+0530 (India Standard Time)

1.This is a complete date format using ISO date.

let val = new Date("2020-08-01");
  • Sat Aug 01 2020 05:30:00 GMT+0530 (India Standard Time)

2.In this format, we specify only year and month (YYYY-MM) without day.

let val = new Date("2020-08");
  • Sat Aug 01 2020 05:30:00 GMT+0530 (India Standard Time)

JavaScript Short Date

  • The "MM/DD/YYYY" is the format used to write short dates. Now, we understand the short date by using an example.
javascript-short-date

Sample Code

<html>  
<head>  
</head>  
<body>  
<div>  
<h1> Welcome to the Wikitechy </h1>  
<h3> It is an example of JavaScript's Short date </h3>  
</div>  
<script>  
let val = new Date("08/01/2020");  
document.write(val);  
</script>  
</body>  
</html>

Output

Welcome to the Wikitechy
It is an example of JavaScript's Short date
Sat Aug 01 2020 00:00:00 GMT+0530 (India Standard Time)

JavaScript Long Date

  • The "MMM DD YYYY" is the format used to write Long dates. The month and date can be written in any order, and it is allowed to write a month in abbreviated (Aug) form or in full (August).
javascript-long-date

Sample Code

<!DOCTYPE html>  
<html>  
<head>  
</head>  
<body>  
<div>  
<h1> Welcome to the  wikitechy </h1>  
<h3> It is an example of JavaScript's Long date </h3>  
</div>  
<script>  
let val = new Date("Aug 01 2020");  
document.write(val);  
</script>  
</body>  
</html>  

Output

Welcome to the wikitechy
It is an example of JavaScript's Long date
Sat Aug 01 2020 00:00:00 GMT+0530 (India Standard Time)

Related Searches to JavaScript Date Format - How do I format a date in JavaScript