JavaScript Date Format

In JavaScript, date manipulation is done using the Date object. To create a new Date object, you need to call the new Date() constructor. By default, almost every method on the Date object returns the date and time in the local time zone. There are various methods for converting a Date object to a string. If you want to get the date and time as a string, you can use the toLocaleDateString() method. For more verbose output, you can call the toString() method. In this JavaScript Date Format example, we display the current date and time, using the toLocaleDateString() method. Click on Execute to run JavaScript Date Format Example online and see the results.
JavaScript Date Format Execute
let now = new Date().toLocaleString();

console.log(now)
Updated: Viewed: 5787 times

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:

JavaScript ISO Dates Examples
let now = new Date("2022-08-30");
let year = new Date("2022");
let yearMonth = new Date("2022-08");
let dateUTC = new Date("2022-08-30T12:00:00Z");
let dateEST = new Date("2022-08-30T12:00:00-04:00");

console.log(now);

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:

JavaScript Long Date Example
let longDate1 = new Date("August 30 2022");
let longDate2 = new Date("Aug 30 2022");
let longDate3 = new Date("30 Aug 2022");

console.log(longDate1);

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:

JavaScript Short Date Example
let shortDate = new Date("8/30/2022");

console.log(shortDate);

How to get the current date and time in JavaScript?

Below is an example of getting the current date and time in JavaScript:

JavaScript Date Format Example
let now = new Date();

console.log(now);

How to create a specific date in JavaScript?

The following is an example of creating a specific date in JavaScript:

JavaScript Specific Date Example
let data = new Date("2022-06-24");

console.log(data);

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.

JavaScript Individual Components Date Example
let newDate = new Date();

console.log(newDate.getFullYear());
console.log(newDate.getMonth());
console.log(newDate.getDate());
console.log(newDate.getHours());
console.log(newDate.getMinutes());
console.log(newDate.getSeconds());
console.log(newDate.getMilliseconds());

See also