toLocaleString with Custom Format Options
You'll learn how to combine date and time formatting with fine-grained control over which components appear and how they're styled.
What This Lets You Do
While toLocaleDateString and toLocaleTimeString give you separate control over date and time formatting, toLocaleString with custom options lets you build exactly the output you want by combining both date and time components with precise formatting rules.
Think of it like ordering a custom sandwich: instead of choosing "breakfast" or "lunch" presets, you specify exactly which ingredients (weekday, year, month, day, hour, minute) and how they're prepared (numeric, 2-digit, long, short).
The Syntax
const date = new Date('2024-03-15T14:30:00');
// Combine date and time with custom formats
const formatted = date.toLocaleString('en-US', {
weekday: 'long', // "Friday"
year: 'numeric', // "2024"
month: 'short', // "Mar"
day: '2-digit', // "15"
hour: 'numeric', // "2"
minute: '2-digit', // "30"
hour12: true // AM/PM format
});
console.log(formatted); // "Friday, Mar 15, 2024, 2:30 PM"
Mix and Match Components
You can include only the parts you need:
const casual = date.toLocaleString('en-US', {
weekday: 'short',
month: 'long',
day: 'numeric',
hour: 'numeric',
minute: '2-digit'
});
// "Fri, March 15, 2:30 PM"
Common Option Values
- weekday:
'narrow','short','long' - year:
'numeric','2-digit' - month:
'numeric','2-digit','narrow','short','long' - day:
'numeric','2-digit' - hour/minute/second:
'numeric','2-digit'
Key Takeaway: Use toLocaleString with an options object to cherry-pick exactly which date and time components you want, formatted precisely how you need them, all in one method call.