Skip to content

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
JavaScript

JavaScript: Use Intl API for Native Date, Number, and String Localization

- 30.05.26 - ErcanOPAK

🌍 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.”

— Frontend Architect

Related posts:

JavaScript: Use structuredClone() to Deep Copy Objects

Why setTimeout(fn, 0) Is NOT Immediate (And Breaks Logic)

JavaScript: Use replaceAll() Instead of regex.replace() for Simple Text Replacement

Post Views: 4

Post navigation

HTML: Use Contenteditable to Make Any Element Editable
Ajax: Use Web Workers for CPU-Intensive Tasks Without Freezing UI

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

June 2026
M T W T F S S
1234567
891011121314
15161718192021
22232425262728
2930  
« May    

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (953)
  • How to add default value for Entity Framework migrations for DateTime and Bool (882)
  • Get the First and Last Word from a String or Sentence in SQL (838)
  • How to select distinct rows in a datatable in C# (808)
  • How to make theater mode the default for Youtube (805)
  • Add Constraint to SQL Table to ensure email contains @ (580)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (579)
  • Average of all values in a column that are not zero in SQL (538)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (505)
  • Find numbers with more than two decimal places in SQL (454)

Recent Posts

  • C#: Use String Interpolation Instead of Concatenation
  • C#: Use Tuples to Return Multiple Values from Methods
  • SQL: Use ISNULL and NULLIF for Smart NULL Handling
  • .NET Core: Use Data Annotations for Model Validation
  • Git: Use Git Clean to Remove Untracked Files
  • Ajax: Add Custom Headers to Fetch Requests
  • JavaScript: Use console.table to Display Arrays as Tables
  • HTML: Use Spellcheck Attribute to Enable Browser Spell Check
  • CSS: Use user-select to Prevent Text Selection
  • Windows 11: Use Snipping Tool for Instant Screenshots

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (953)
  • How to add default value for Entity Framework migrations for DateTime and Bool (882)
  • Get the First and Last Word from a String or Sentence in SQL (838)
  • How to select distinct rows in a datatable in C# (808)
  • How to make theater mode the default for Youtube (805)

Recent Posts

  • C#: Use String Interpolation Instead of Concatenation
  • C#: Use Tuples to Return Multiple Values from Methods
  • SQL: Use ISNULL and NULLIF for Smart NULL Handling
  • .NET Core: Use Data Annotations for Model Validation
  • Git: Use Git Clean to Remove Untracked Files

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com