JavaScript Date Formats

Learn how to format dates in JavaScript

πŸ“… What are Date Formats?

JavaScript Date formats determine how dates are displayed and interpreted. You can format dates in many different ways to match your needs.


// Create a new date
let today = new Date();
console.log(today); // Full date and time
                                    

Common Date Formats

πŸ“

ISO Format

Standard international format

// YYYY-MM-DD
let date = new Date("2024-03-15");
console.log(date.toISOString().split('T')[0]);
// Output: "2024-03-15"
πŸ‡ΊπŸ‡Έ

US Format

Month/Day/Year format

// MM/DD/YYYY
let date = new Date("03/15/2024");
console.log(date.toLocaleDateString('en-US'));
// Output: "3/15/2024"
🌍

European Format

Day/Month/Year format

// DD/MM/YYYY
let date = new Date("2024-03-15");
console.log(date.toLocaleDateString('en-GB'));
// Output: "15/03/2024"
πŸ“–

Long Format

Full readable date

// Full date string
let date = new Date("2024-03-15");
console.log(date.toDateString());
// Output: "Fri Mar 15 2024"

πŸ”Ή Creating Dates with Different Formats

You can create dates using various input formats:

// Different ways to create dates
let date1 = new Date("2024-03-15");           // ISO format
let date2 = new Date("March 15, 2024");       // Long format
let date3 = new Date("03/15/2024");           // US format
let date4 = new Date(2024, 2, 15);            // Year, month (0-based), day

console.log(date1); // Fri Mar 15 2024
console.log(date2); // Fri Mar 15 2024
console.log(date3); // Fri Mar 15 2024
console.log(date4); // Fri Mar 15 2024

Output:

All dates create: Fri Mar 15 2024 00:00:00 GMT...

πŸ”Ή Formatting Date Output

Use built-in methods to format date output:

let date = new Date("2024-03-15");

// Different formatting methods
console.log(date.toString());        // Full date string
console.log(date.toDateString());    // Date only
console.log(date.toTimeString());    // Time only
console.log(date.toISOString());     // ISO format
console.log(date.toLocaleDateString()); // Local date format
console.log(date.toLocaleTimeString()); // Local time format

Output:

Fri Mar 15 2024 00:00:00 GMT-0500 (EST)
Fri Mar 15 2024
00:00:00 GMT-0500 (EST)
2024-03-15T05:00:00.000Z
3/15/2024
12:00:00 AM

πŸ”Ή Custom Date Formatting

Create your own date format using date methods:

let date = new Date("2024-03-15");

// Custom formatting function
function formatDate(date) {
    let day = date.getDate().toString().padStart(2, '0');
    let month = (date.getMonth() + 1).toString().padStart(2, '0');
    let year = date.getFullYear();
    
    return `${day}/${month}/${year}`;
}

console.log(formatDate(date)); // 15/03/2024

// Another custom format
function formatDateLong(date) {
    let months = ['January', 'February', 'March', 'April', 'May', 'June',
                  'July', 'August', 'September', 'October', 'November', 'December'];
    
    let day = date.getDate();
    let month = months[date.getMonth()];
    let year = date.getFullYear();
    
    return `${day} ${month} ${year}`;
}

console.log(formatDateLong(date)); // 15 March 2024

Output:

15/03/2024
15 March 2024

πŸ”Ή Locale-Specific Formatting

Format dates according to different locales:

let date = new Date("2024-03-15");

// Different locale formats
console.log(date.toLocaleDateString('en-US'));    // 3/15/2024
console.log(date.toLocaleDateString('en-GB'));    // 15/03/2024
console.log(date.toLocaleDateString('de-DE'));    // 15.3.2024
console.log(date.toLocaleDateString('fr-FR'));    // 15/03/2024
console.log(date.toLocaleDateString('ja-JP'));    // 2024/3/15

// With options
let options = { 
    year: 'numeric', 
    month: 'long', 
    day: 'numeric' 
};

console.log(date.toLocaleDateString('en-US', options));
// March 15, 2024

Output:

3/15/2024
15/03/2024
15.3.2024
15/03/2024
2024/3/15
March 15, 2024

🧠 Test Your Knowledge

Which method returns the date in ISO format?