JavaScript Date Formats

Back to JS page

Table of Contents

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:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Date Formats</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>JavaScript ISO Dates</h4>
<p id="demo"></p>

<script>
// Complete date (year-month-day)
const completeDate = new Date("2024-03-15");

// Year and month (yyyy-mm)
const yearMonth = new Date("2024-03");

// Only year (yyyy)
const onlyYear = new Date("2024");

document.getElementById("demo").innerHTML =
  "new Date('2024-03-15'): " + completeDate + "<br>" +
  "new Date('2024-03'): " + yearMonth + "<br>" +
  "new Date('2024'): " + onlyYear;
</script>

</body>
</html>

Example 1

Result View Example

ISO Dates (Date-Time)

ISO dates can be written with added hours, minutes, and seconds (YYYY-MM-DDTHH:MM:SSZ):

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Date Formats</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>ISO Dates (Date-Time)</h4>
<p id="demo"></p>

<script>
// Date and time with T separator
const dateTime = new Date("2024-03-15T10:30:00");

// Date and time with UTC (Z = Zulu time / UTC)
const utcTime = new Date("2024-03-15T10:30:00Z");

// Date and time with timezone offset
const timezoneTime = new Date("2024-03-15T10:30:00+07:00");

document.getElementById("demo").innerHTML =
  "With T separator: " + dateTime + "<br>" +
  "With Z (UTC): " + utcTime + "<br>" +
  "With +07:00 offset: " + timezoneTime;
</script>

</body>
</html>

Example 2

Result View Example

JavaScript Short Dates

Short dates are written with the "MM/DD/YYYY" syntax. Be careful with month/day order:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Date Formats</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>JavaScript Short Dates</h4>
<p id="demo"></p>

<script>
// Short date format: MM/DD/YYYY
const shortDate1 = new Date("03/15/2024");   // March 15, 2024

// Short date with dashes
const shortDate2 = new Date("2024-03-15");

// Short date or long date
const shortDate3 = new Date("March 15, 2024");

document.getElementById("demo").innerHTML =
  "MM/DD/YYYY: " + shortDate1 + "<br>" +
  "YYYY-MM-DD: " + shortDate2 + "<br>" +
  "Month name: " + shortDate3;
</script>

</body>
</html>

Example 3

Result View Example

JavaScript Long Dates

Long dates are most often written with a "MMM DD YYYY" syntax. Month and day can be in any order:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Date Formats</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>JavaScript Long Dates</h4>
<p id="demo"></p>

<script>
// Long date formats
const longDate1 = new Date("Mar 15 2024");          // Month name short
const longDate2 = new Date("March 15, 2024");       // Full month name
const longDate3 = new Date("15 Mar 2024");          // Day first

// With time
const longDate4 = new Date("March 15, 2024 10:30:00");

document.getElementById("demo").innerHTML =
  "Mar 15 2024: " + longDate1 + "<br>" +
  "March 15, 2024: " + longDate2 + "<br>" +
  "15 Mar 2024: " + longDate3 + "<br>" +
  "March 15, 2024 10:30:00: " + longDate4;
</script>

</body>
</html>

Example 4

Result View Example

Date Input - Parsing Dates

If you have a valid date string, you can use the Date.parse() method to convert it to milliseconds and back to a Date object:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Date Formats</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Date Input - Parsing Dates</h4>
<p id="demo"></p>

<script>
// Date.parse() converts a date string to milliseconds
let ms = Date.parse("March 15, 2024");
const parsedDate = new Date(ms);

// Parse ISO format
let ms2 = Date.parse("2024-03-15T10:30:00");
const parsedDate2 = new Date(ms2);

document.getElementById("demo").innerHTML =
  "Date.parse('March 15, 2024'): " + ms + " ms<br>" +
  "new Date(ms): " + parsedDate + "<br><br>" +
  "Date.parse('2024-03-15T10:30:00'): " + ms2 + " ms<br>" +
  "new Date(ms2): " + parsedDate2;
</script>

</body>
</html>

Example 5

Result View Example

Document

Document in project

You can Download PDF file.

Reference