🌍 No More Moment.js
Dates, numbers, currencies, plurals — all have different formats per locale. Intl API is built-in. No libraries needed.
📝 Date Formatting
const date = new Date();
// US English
new Intl.DateTimeFormat('en-US').format(date);
// "12/31/2024"
// UK English
new Intl.DateTimeFormat('en-GB').format(date);
// "31/12/2024"
// German
new Intl.DateTimeFormat('de-DE').format(date);
// "31.12.2024"
// Japanese
new Intl.DateTimeFormat('ja-JP').format(date);
// "2024/12/31"
// With options
new Intl.DateTimeFormat('en-US', {
weekday: 'long',
year: 'numeric',
month: 'long',
day: 'numeric'
}).format(date);
// "Tuesday, December 31, 2024"
// Relative time
const rtf = new Intl.RelativeTimeFormat('en', { numeric: 'auto' });
rtf.format(-1, 'day'); // "yesterday"
rtf.format(2, 'day'); // "in 2 days"
rtf.format(-3, 'week'); // "3 weeks ago"
đź’° Number & Currency Formatting
const price = 1234.56;
// US Dollar
new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD'
}).format(price);
// "$1,234.56"
// Euro (German)
new Intl.NumberFormat('de-DE', {
style: 'currency',
currency: 'EUR'
}).format(price);
// "1.234,56 €"
// Japanese Yen (no decimals)
new Intl.NumberFormat('ja-JP', {
style: 'currency',
currency: 'JPY',
minimumFractionDigits: 0
}).format(price);
// "¥1,235"
// Percentage
new Intl.NumberFormat('en-US', {
style: 'percent',
minimumFractionDigits: 1
}).format(0.1234);
// "12.3%"
// Compact notation (1K, 1M)
new Intl.NumberFormat('en-US', {
notation: 'compact',
compactDisplay: 'short'
}).format(1234567);
// "1.2M"
âś… String Operations
// Plurals (German example)
new Intl.PluralRules('de').select(0); // "zero"
new Intl.PluralRules('de').select(1); // "one"
new Intl.PluralRules('de').select(2); // "other"
// List formatting
const listFormatter = new Intl.ListFormat('en', {
style: 'long',
type: 'conjunction'
});
listFormatter.format(['Apple', 'Banana', 'Orange']);
// "Apple, Banana, and Orange"
// Sort strings (respects language)
const words = ['café', 'apple', 'banana', 'éclair'];
words.sort(new Intl.Collator('fr').compare);
// French sorting: apple, banana, café, éclair
// Segmenter (split by word/sentence)
const segmenter = new Intl.Segmenter('en', { granularity: 'word' });
const segments = segmenter.segment('Hello world');
for (let seg of segments) {
console.log(seg.segment);
}
đź’ˇ Benefits
- No external libraries (moment.js, numeral.js) → smaller bundle
- Native performance (C++ implementation)
- Supports 100+ languages
- Automatic locale detection: navigator.language
“Removed moment.js (70KB). Used Intl.DateTimeFormat. Bundle size decreased 15%. Dates now handle all user locales automatically. Native API wins.”
