JavaScript Date Formats

JavaScript provides multiple ways to format dates for display or processing. The Date object offers methods to format dates as strings in various formats.

1. Standard Date Formats

1.1. ISO 8601 Format

The ISO 8601 standard is a commonly used date format that is easily parsed and understood by different systems.

let date = new Date();
console.log(date.toISOString());  // Output: 2025-01-19T10:20:30.000Z

Try It Now

Characteristics:

  • Format: YYYY-MM-DDTHH:mm:ss.sssZ
  • It’s time zone-independent (Z indicates UTC).

1.2. Short Date Format

console.log(date.toLocaleDateString());  // Output: 1/19/2025 (based on locale)

Try It Now

Characteristics:

  • Format varies by locale (e.g., MM/DD/YYYY in the US).

1.3. Long Date Format

console.log(date.toDateString());  // Output: Sun Jan 19 2025

Try It Now

Characteristics:

  • Displays the day of the week, month, day, and year.

2. Custom Date Formatting

To format dates in a custom format, you can extract individual components and concatenate them as needed.

Example: Custom Format DD-MM-YYYY

let day = date.getDate();
let month = date.getMonth() + 1;  // Months are zero-indexed
let year = date.getFullYear();

let formattedDate = `${day}-${month}-${year}`;
console.log(formattedDate);  // Output: 19-1-2025

Try It Now

3. Formatting with Intl.DateTimeFormat

The Intl.DateTimeFormat object allows you to format dates based on the locale and options you provide.

Example: Using Intl.DateTimeFormat

let options = { year: 'numeric', month: 'long', day: 'numeric' };
let formattedDate = new Intl.DateTimeFormat('en-US', options).format(date);
console.log(formattedDate);  // Output: January 19, 2025

Try It Now

Customizable Options:

  • year: 'numeric', '2-digit'
  • month: 'numeric', '2-digit', 'long', 'short', 'narrow'
  • day: 'numeric', '2-digit'

4. Parsing Dates from Strings

You can create Date objects from strings using the Date constructor.

Example: Parsing an ISO String

let isoDate = new Date('2025-01-19T10:20:30Z');
console.log(isoDate);  // Output: Sun Jan 19 2025 10:20:30 GMT+0000 (UTC)

Try It Now

Example: Parsing a Custom Date String

let customDate = new Date('January 19, 2025 10:20:30');
console.log(customDate);  // Output: Sun Jan 19 2025 10:20:30 GMT+0000 (UTC)

Try It Now

5. Common Date Formatting Methods

  • toDateString(): Returns the date portion of a Date object as a string.
    console.log(date.toDateString());  // Output: Sun Jan 19 2025
    

    Try It Now

  • toTimeString(): Returns the time portion of a Date object as a string.
    console.log(date.toTimeString());  // Output: 10:20:30 GMT+0000 (UTC)
    

    Try It Now

  • toLocaleString(): Returns a string with a language-sensitive representation of the date and time.
    console.log(date.toLocaleString());  // Output: 1/19/2025, 10:20:30 AM
    

    Try It Now

Summary of JavaScript Date Formats

  • Standard Formats: Use toISOString(), toDateString(), toLocaleDateString(), etc.
  • Custom Formats: Extract and concatenate date components manually.
  • Localization: Use Intl.DateTimeFormat for locale-specific formatting.
  • Parsing Dates: Create Date objects from strings for flexibility in date handling.