native-dates

Examples / Localization

Arabic plurals, numerals, and Hijri dates

Format Arabic six-way plural durations, Eastern and Western numeral systems, regional month names, and Umm al-Qura Hijri dates.

Try this

  1. Click 1, 2, 5, 11, and 100 to step through Arabic's singular, dual يومان, plural, accusative, and hundredth noun forms.
  2. Compare ar-EG and ar-MA on the same date to see Eastern Arabic digits alongside Western digits and Moroccan month names.
  3. Pick different dates to watch ar-SA-u-ca-islamic-umalqura convert ISO dates into the Umm al-Qura Hijri calendar.
import { configure, date, days } from 'native-dates';

configure({ locale: 'ar-EG' });

const dayInput = document.querySelector('#day');
const count = document.querySelector('#count');
const output = document.querySelector('#output');

// isolate each right-to-left run so digits and range dashes stay in Arabic order
const rtl = (text) => `<bdi dir="rtl">${text}</bdi>`;

function render() {
  const total = Number(count.value);
  if (!dayInput.value || !(total >= 1)) { return; }
  const day = date(dayInput.value);
  const span = day.to(day.plus(4, 'days'));
  const maghreb = day.locale('ar-MA');

  const rows = [
    ['Six-way plural', '.text', rtl(days(total).text)],
    [
      'Eastern digits',
      ".format('long')",
      `${rtl(day.format('long'))} <span class="muted">·</span> ${rtl(span.format('long'))}`,
    ],
    [
      'Maghrebi locale',
      ".locale('ar-MA')",
      `${rtl(maghreb.format('long'))} <span class="muted">·</span> ${rtl(maghreb.for(5, 'days').format('long'))}`,
    ],
    [
      'Umm al-Qura Hijri',
      'ar-SA-u-ca-islamic-umalqura',
      `${rtl(day.format('long', 'ar-SA-u-ca-islamic-umalqura'))} <span class="muted">·</span> ${rtl(day.monthDay.format('long'))}`,
    ],
  ];

  output.innerHTML = rows.map(([label, locale, value]) => `
    <li>
      <span class="label">${label} <code>${locale}</code></span>
      <b>${value}</b>
    </li>`).join('');
}

for (const button of document.querySelectorAll('[data-count]')) {
  button.addEventListener('click', () => {
    count.value = button.dataset.count;
    render();
  });
}

dayInput.addEventListener('input', render);
count.addEventListener('input', render);
render();

Starting…

How it works

Calling configure({ locale: 'ar-EG' }) once enables Arabic plurals and Eastern Arabic digits across every value. Reading days(2).text outputs a true dual noun with no digit at all.

Attach .locale('ar-MA') when a value and its derived ranges should use Western digits and Moroccan month names while the rest of the app keeps the configured default.

Passing 'ar-SA-u-ca-islamic-umalqura' converts any civil date into Saudi Arabia’s Umm al-Qura Hijri calendar, while day.monthDay keeps a Gregorian reference alongside it.

When embedding Arabic text inside a left-to-right page, wrap each formatted value in <bdi> so digits and range dashes keep right-to-left order.