UAE labour law (Federal Decree-Law No. 33 of 2021, Article 51) defines end-of-service gratuity with a two-tier formula. I turned it into a free web tool — here's the exact logic, including the edge cases most implementations get wrong.
Live tool: https://thegratuitycalculatoruae.com/
Repo: https://github.com/jackkkwesttt-byte/Gratuity-calculator-uae
The formula
Daily wage = basic monthly salary ÷ 30
First 5 years of service: 21 days' wage per year
Every year after 5: 30 days' wage per year
Eligibility: minimum 1 full year of service
Cap: total can never exceed 2 years of basic salary
Unpaid leave days are excluded from the service count
The implementation
function calculateGratuity(basicSalary, yearsOfService, unpaidLeaveDays = 0) {
// No gratuity under one year of service
if (yearsOfService < 1) return 0;
const dailyWage = basicSalary / 30;
// Unpaid leave doesn't count toward service
const effectiveYears = yearsOfService - unpaidLeaveDays / 365;
const firstTier = Math.min(effectiveYears, 5) * 21 * dailyWage;
const secondTier = Math.max(effectiveYears - 5, 0) * 30 * dailyWage;
const total = firstTier + secondTier;
// Statutory cap: 2 years of basic salary
return Math.min(total, basicSalary * 24);
}
// Example: AED 8,000 basic, 3 years service
console.log(calculateGratuity(8000, 3)); // 16800
Edge cases worth handling
Fractional years. Someone with 5 years and 4 months isn't at the 30-day tier for the partial year — the split happens exactly at the 5-year mark. Math.min/Math.max on the tier boundary handles this; don't round years first.
The cap. Easy to forget. A 30-year employee at AED 20,000/month computes 855 days' wage ≈ AED 570,000 — but the statute caps it at 24 months = AED 480,000. The cap only binds past ~25.5 years of service, which is exactly why it gets forgotten. Always apply it.
Basic vs gross salary. This is a data problem, not a math problem — the formula only ever takes basic salary. If your UI has one salary field, label it "Basic Salary (excluding allowances)" or users will inflate their own result.
Unpaid leave. Most calculators ignore this. Subtracting unpaid days from the service period is the legally correct behavior.
Why this was fun to build
It's a rare case where the entire business logic fits in 15 lines but the domain edge cases (tier boundary, cap, leave exclusion) are where correctness lives. The UI is the easy part — getting the law right in code is the actual work.
If you're in the UAE private sector, run your own numbers against your settlement letter: https://thegratuitycalculatoruae.com/
Top comments (0)