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.
Contents
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:
You can write ISO dates without defining the day (YYYY-MM):
You can also write ISO dates without naming day and month (YYYY):
The model for writing JavaScript time contains seconds, minutes, and hours (YYYY-MM-DDTHH:MM:SSZ):
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:
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.
- Easy to use with a learn-by-doing approach
- Offers quality content
- Gamified in-browser coding experience
- The price matches the quality
- Suitable for learners ranging from beginner to advanced
- Free certificates of completion
- Focused on data science skills
- Flexible learning timetable
- Simplistic design (no unnecessary information)
- High-quality courses (even the free ones)
- Variety of features
- Nanodegree programs
- Suitable for enterprises
- Paid Certificates of completion
- A wide range of learning programs
- University-level courses
- Easy to navigate
- Verified certificates
- Free learning track available
- University-level courses
- Suitable for enterprises
- Verified certificates of completion
Short Dates
An "MM/DD/YYYY" syntax is used to write short dates.
Warnings
Skipping leading zeroes on days or months may cause an error in some browsers:
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:
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:
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:
A month can be written abbreviated (Jan) or full (January):
The names are case insensitive, and commas are ignored:
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.