format
navigator.language
navigator.languages
Accept-Language
** Client-side JavaScript can't read HTTP request headers.
See the Pen navigator.language(s) by Raymond Camden (@cfjedimaster) on CodePen.
Intl.DateTimeFormat
let now = new Date();
// defaults for everything
console.log(new Intl.DateTimeFormat().format(now));
let now = new Date();
console.log(new Intl.DateTimeFormat('en-GB').format(now));
let now = new Date();
let tomorrow = new Date().setDate(now.getDate() + 1);
let formatter = new Intl.DateTimeFormat();
console.log(`
Today: ${formatter.format(now)} -
Tomorrow: ${formatter.format(tomorrow)}
`);
let now = new Date();
let formatter = new Intl.DateTimeFormat('en-US', {
dateStyle:'long',
timeStyle:'long'
});
console.log(formatter.format(now));
let now = new Date();
let formatter = new Intl.DateTimeFormat('en-US', {
dateStyle:'short',
timeStyle:'short'
});
console.log(formatter.format(now));
See the Pen navigator.language(s) by Raymond Camden (@cfjedimaster) on CodePen.
let now = new Date();
let formatter = new Intl.DateTimeFormat('en-US', {
year:'2-digit',
month:'short',
day:'2-digit',
dayPeriod:'long'
});
console.log(formatter.format(now));
See the Pen DateTime Formatting Options by Raymond Camden (@cfjedimaster) on CodePen.
Intl.RelativeTimeFormat
let formatter = new Intl.RelativeTimeFormat('en', {
style:"long"
});
console.log(formatter.format(-1,"day"));
console.log(formatter.format(1,"minute"));
See the Pen DateTime Range Example by Raymond Camden (@cfjedimaster) on CodePen.
What do you pick for the units?
What number do you use?
Using Intl.RelativeTimeFormat for Localized Relative Timings
Intl.DurationFormat
let duration = {
hours: 28,
minutes: 19
}
console.log(
new Intl.DurationFormat('en', { style:"long" }).format(duration)
);
let duration = {
hours: 28,
minutes: 19
}
console.log(
new Intl.DurationFormat('en',
{ style:"long", daysDisplay:'always' }).format(duration)
);
let duration = {
hours: 28,
minutes: 19
}
console.log(
new Intl.DurationFormat('es', { style:"long" }).format(duration)
);
See the Pen Duration Example by Raymond Camden (@cfjedimaster) on CodePen.
When is 2/9/2024?
Intl.NumberFormat
// modern browsers let you use _ to help read large numbers
// info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#numeric_separators
let input = 544_194_391;
console.log(
new Intl.NumberFormat('en', { style:"decimal" }).format(input)
);
let input = 544_194_391;
console.log(
new Intl.NumberFormat('fr', { style:"decimal" }).format(input)
);
let input = 1;
console.log(
new Intl.NumberFormat('en', { minimumIntegerDigits:2 }).format(input)
);
let input = 544_194_391;
console.log(
new Intl.NumberFormat('en', { maximumSignificantDigits:4 }).format(input)
);
let input = 544_194_391;
console.log(
new Intl.NumberFormat('en', { notation:'compact' }).format(input)
);
let input = 544_194_391;
console.log(
new Intl.NumberFormat('en',
{ notation:'compact', compactDisplay:'long' }).format(input)
);
.formatRange()
let a = 9_000;
let b = 999_999;
console.log(
new Intl.NumberFormat('en',
{ notation:'compact', compactDisplay:'long' })
.formatRange(a,b)
);
See the Pen Format Range for Numbers by Raymond Camden (@cfjedimaster) on CodePen.
let input = 544_194_391;
console.log(
new Intl.NumberFormat('en',
{ style:"currency", currency:"USD" }).format(input)
);
let input = 544_194_391;
console.log(
new Intl.NumberFormat('fr',
{ style:"currency", currency:"USD" }).format(input)
);
let input = 544_194_391;
console.log(
new Intl.NumberFormat('en',
{ style:"currency", currency:"USD", notation:"compact" }).format(input)
);
let input = 544_194_391;
console.log(
new Intl.NumberFormat('en',
{
style:"currency", currency:"USD",
notation:"compact", currencyDisplay:'name' }).format(input)
);
Intl.ListFormat
let input = ['cats','dogs','books'];
console.log(
new Intl.ListFormat('en').format(input)
);
let input = ['cats','dogs','books'];
console.log(
new Intl.ListFormat('es').format(input)
);
See the Pen ListFormat by Raymond Camden (@cfjedimaster) on CodePen.
select
methodSee the Pen PluralRules by Raymond Camden (@cfjedimaster) on CodePen.
If we were to use String.prototype.split(" ") to segment a text in words, we would not get the correct result if the locale of the text does not use whitespaces between words (which is the case for Japanese, Chinese, Thai, Lao, Khmer, Myanmar, etc.).
See the Pen Segmenter by Raymond Camden (@cfjedimaster) on CodePen.