Hijri Age Calculation in the Browser — Umm al-Qura, Intl.DateTimeFormat, and the Bugs That Bit Us

2026-11-30来源 dev.toEnglish

A few months ago a user wrote to us, politely confused. Our calculator told him he was 34. His family insisted he was 33.

Both were right. He was 33 in Gregorian years and 34 in Hijri years — and that one message basically explains why our little Arabic age calculator, Hasibat Alomr, shows two ages side by side instead of one.

If you build anything date-related for users in Saudi Arabia or the Gulf, you run into this fast. Official life there — birth certificates, school enrollment, retirement paperwork — runs on the Hijri calendar. Phones and laptops run on Gregorian. People genuinely live on both, so our tools had to as well.

This post is about the three bugs that taught me how different the two calendars really are. Two of them were my fault. One of them was the calendar's.

Hijri years are shorter, and it adds up

The Hijri calendar is lunar. A normal year has 354 days, a leap year 355 — about 11 days less than a Gregorian year. That sounds small until you compound it over a lifetime: Hijri age grows roughly 3% faster, so the gap widens by about a year every 33 years.

This is also why the "just multiply by 0.97" shortcut you see in forum answers is wrong. It's close for a 10-year-old and noticeably off for a 60-year-old. We tried it in the first prototype. The error is exactly what our polite user found.

Saudi Arabia's official calendar is the Umm al-Qura calendar, and its month lengths don't follow a formula you can hard-code — a month is 29 or 30 days based on astronomical calculation. You need a real calendar implementation, not a lookup table you typed from Wikipedia. (Ask me how I know.)

The good news: the browser ships the calendar

Modern browsers include Umm al-Qura inside Intl.DateTimeFormat, through the islamic-umalqura extension:

const hijriFmt = new Intl.DateTimeFormat('en-u-ca-islamic-umalqura', { day: 'numeric', month: 'numeric', year: 'numeric', timeZone: 'Asia/Riyadh' }); hijriFmt.format(new Date(Date.UTC(2000, 2, 15, 12))); // "20/11/1420" — 20 Dhu al-Qa'dah 1420 AH 

That one API replaced the lookup-table approach entirely. For the age itself we convert the birth date and today into Hijri parts with formatToParts, then do the schoolbook subtraction — years, months, days, borrowing when a number goes negative. Same algorithm as a Gregorian age calculator. The devil is in what "borrowing" means when months don't have fixed lengths, which brings me to the bugs.

Bug 1: I built dates at midnight, and midnight moved

Version one created dates at UTC midnight and formatted them without thinking about time zones. For anyone west of Greenwich, UTC midnight is still the previous evening — so results were off by a day, and the calculator wished people a happy birthday a day early. Not a great look for an age calculator.

The fix is boring and total: build the instant at midday UTC (Date.UTC(y, m, d, 12)) so no realistic offset crosses a date line, and always format with timeZone: 'Asia/Riyadh' so "today" means the user's today.

Bug 2: borrowing days from a month of unknown length

When the day subtraction goes negative, you borrow the length of the previous month. Easy in Gregorian. In Hijri, the previous month might have 29 or 30 days and there's no pattern to memorize.

Our workaround is a small trick I'm still a bit pleased with: convert the 1st of the current Hijri month to a Gregorian date, step back one day, convert that back to Hijri. The day number you land on is the length of the previous month. Three Intl calls. For a calculator that runs once per click, performance is a non-issue.

Bug 3: the digits weren't digits

This one came from real users pasting dates with Arabic-Indic numerals (٠١٢٣٤٥٦٧٨٩) or Persian digits. Our parser read them as garbage and failed silently — the worst kind of failure. Now we normalize before parsing:

const normalizeDigits = s => s .replace(/[٠-٩]/g, d => '٠١٢٣٤٥٦٧٨٩'.indexOf(d)) .replace(/[۰-۹]/g, d => '۰۱۲۳۴۵۶۷۸۹'.indexOf(d)); 

Ten lines. Should have been there from day one.

Where this ended up

The Hijri age calculator now runs the whole thing in the browser — both ages, side by side, and the birth date never leaves the device, which matters when the date belongs to a real person and not a test fixture. The same conversion code drives our Gregorian calculator, the age-difference tool and the date converter.

If I had to compress it into advice: don't approximate a lunar calendar with a ratio, don't trust midnight, and don't assume a digit is a digit.

Has anyone else here shipped something with islamic-umalqura? I'm curious what broke for you — I suspect everyone's bug list looks a little different.

原文:https://www.a8q9.cn/en/article/hijri-age-calculation-in-the-browser-umm-al-qura-intl-datetimeformat-and-the-bug-f83b24.html

相关文章