Types of JavaScript Date Formats
There are three types of date formats in JavaScript: ISO (International Organization for Standardization) dates, short dates, and long dates. By default, JavaScript uses ISO dates internally. Below are the date formats in JavaScript with a detailed description:
ISO Dates
The ISO dates use the ISO 8601 standard to specify dates and times differently. By default, JavaScript uses this standard because it is well-defined, precise, and consistent.
According to ISO 8601, dates consist of the following parts:
- MM - a month from 01 to 12
- MMM - month abbreviation from january to december
- DD - day from 01 to the last day of the month
- YYYY - year as a 4-digit number
According to ISO 8601, the parts of time are as follows:
- T - separation of character between date and time
- HH - hours from 00 to 23
- MM - minutes from 00 to 59
- SS - seconds from 00 to 59
- Z - indicates Coordinated Universal Time (UTC)
- +HH:MM - replaces "Z" if offsetting UTC to another time zone later than UTC
- HH:MM - replaces "Z" if offsetting UTC to another time zone earlier than UTC
The following are examples of the various ISO date and time formats that are supported in JavaScript:
Long Dates
Long dates use the month abbreviation, not the month number. The month and day can be in any of the first positions - "MMM DD YYYY" or "DD MMM YYYY" - so both of these formats are valid. You can write full month names or abbreviate them in long dates. Long dates ignore commas and are not case-sensitive. The following is an example of a Long Date in JavaScript:
Short Dates
The format for short dates is MM/DD/YYYY, which is used every day by most USA. The following is an example of a Short Date in JavaScript:
How to get the current date and time in JavaScript?
Below is an example of getting the current date and time in JavaScript:
How to create a specific date in JavaScript?
The following is an example of creating a specific date in JavaScript:
How to get the individual components of a date and time in JavaScript?
In JavaScript, the following methods are provided to get the individual components of a date and time:
- getFullYear(): returns a year consisting of 4 numbers
- getMonth(): returns the month as a number from 0 to 11 (0 - January, 1 - February, 2 - March, ..., 11 - December)
- getDate(): returns the day of the month from 1 to 31
- getHours(): returns the number of hours from 0 to 23
- getMinutes(): returns the number of minutes from 0 to 59
- getSeconds(): returns the number of seconds from 0 to 59
- getMilliseconds(): returns the number of milliseconds from 0 to 999
All of these methods return separate date and time components according to the time zone set on the user's local device.