Skip to content

Instantly share code, notes, and snippets.

@KevinPy
Last active May 20, 2025 09:23
Show Gist options
  • Select an option

  • Save KevinPy/85c9c90bd16a0c45d80fc6c0ebe41ebd to your computer and use it in GitHub Desktop.

Select an option

Save KevinPy/85c9c90bd16a0c45d80fc6c0ebe41ebd to your computer and use it in GitHub Desktop.
Format lot of things
// ---------------------------------------------
// Number
// ---------------------------------------------
const amount = 1234567.89;
const priceFR = new Intl.NumberFormat(
"fr-FR",
{
style: "currency",
currency: "EUR"
}
).format(amount);
const priceUS = new Intl.NumberFormat(
"en-US",
{
style: "currency",
currency: "USD"
}
).format(amount);
console.log(priceFR); // "1 234 567,89 €"
console.log(priceUS); // "$1,234,567.89"
// ---------------------------------------------
// Date
// ---------------------------------------------
const date = new Date("2021-10-02T09:10:30Z");
const formatted = new Intl.DateTimeFormat(
"fr-FR",
{
weekday: "long",
year: "numeric",
month: "long",
day: "numeric",
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
timeZoneName: "short"
}
).format(date);
console.log(formatted); // "samedi 2 octobre 2021 à 11:10:30 UTC+2"
// ---------------------------------------------
// List
// ---------------------------------------------
const list = ["pomme", "banane", "orange"];
const formattedList = new Intl.ListFormat("fr", {
style: "long",
type: "conjunction"
}).format(list);
console.log(formattedList); // "pomme, banane et orange"
// ---------------------------------------------
// Plural
// ---------------------------------------------
const plural = new Intl.PluralRules("fr");
function formatMessage(count: number) {
const suffix = plural.select(count) === "one" ? "" : "s";
return `Vous avez ${count} message${suffix}`;
}
console.log(formatMessage(1)); // "Vous avez 1 message"
console.log(formatMessage(2)); // "Vous avez 2 messages"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment