Cyber Week

Save big!

All courses under $5 - for a limited time!

Code has been added to clipboard!

Learn About JavaScript Date Format With Examples

Reading time 4 min
Published Aug 8, 2017
Updated Oct 1, 2019

ISO 8601 refers to the accepted standard for date and time representation, which is also a preference in JavaScript. In this tutorial, you will learn about the ISO JavaScript date format, the different ways it is used and displayed in browsers.

JavaScript allows you to add different functionality to websites. Whether you want to display a calendar with events or a date of user's last visit, you have to rely on JavaScript objects. By using them, you can enhance a page with references to time: the output can include hours, minutes, and seconds.

JavaScript Date Format: Main Tips

  • The ISO format is a JavaScript standard.
  • The other formats are browser specific and not well defined.
  • There are 3 formats for JavaScript date input.

Date Output

Take a look at the date below. This is the default way JavaScript format date output is displayed:

Fri Jan 20 2018 03:00:00 GMT+0200 (EET)

As you can see, we have elements of both time (hours, minutes, and seconds) and date (day of the week, day of the month, month, and year) defined. We also see the time zone our browser is using.

ISO Dates

The international standard times and dates representation is ISO 8601. Its syntax (YYYY-MM-DD) is a common JavaScript date format, too:

Example
var d = new Date("2018-12-31");

You can write ISO dates without defining the day (YYYY-MM):

Example
var d = new Date("2018-01");

You can also write ISO dates without naming day and month (YYYY):

Example
var d = new Date("2018");

The model for writing JavaScript time contains seconds, minutes, and hours (YYYY-MM-DDTHH:MM:SSZ):

Example
var d = new Date("2018-01-20T13:30:00Z");

Note: all the dates displayed will be relative to your time zone.

A capital T separates date and time. A capital letter Z defines UTC.

Remove the Z and add -HH:MM or +HH:MM to modify the time relative to UTC:

Example
var d = new Date("2018-01-20T13:30:00-06:00");

Note: GMT (Greenwich Mean Time) is the same as UTC (Universal Time Coordinated). Different results may be displayed in different browsers by omitting Z or T in a date-time string.

Time Zones

JavaScript will use the browser's time zone when setting or getting a date with the unspecified time zone.

It means that if a time/date is created in GMT (Greenwich Mean Time), the time/date will be changed to CDT (Central US Daylight Time) when a central US user connects.

Short Dates

An "MM/DD/YYYY" syntax is used to write short dates.

Example
var d = new Date("01/20/2018");

Warnings

Skipping leading zeroes on days or months may cause an error in some browsers:

Example
var z = new Date("2018-1-20");

The "YYYY/MM/DD" behavior is undefined. Some browsers will return NaN, and some will try to guess the format:

Example
var z = new Date("2018/01/20");

The "DD/MM/YYY" behavior is also undefined. Some browsers will return NaN, and some will try to guess the format:

Example
var z = new Date("20-01-2018");

Long Dates

A "MMM DD YYYY" syntax is a common long date format. As you can see in the examples below, the day and month can be defined in any order:

Example
var d = new Date("Jan 20 2018");
Example
var d = new Date("20 Jan 2018");

A month can be written abbreviated (Jan) or full (January):

Example
var d = new Date("January 20 2018");
Example
var d = new Date("Jan 20 2018");

The names are case insensitive, and commas are ignored:

Example
var d = new Date("JANUARY, 20, 2018");

JavaScript Date Format: Summary

  • JavaScript uses the ISO date format.
  • There are many ways you can modify and display JavaScript time and date.
  • There are 3 formats for JavaScript date input: ISO, short and long.