UNPKG

luxon-hijri

Version:

A Hijri date converter based on the Umm al-Qura calendar system, using Luxon for date manipulations.

64 lines (63 loc) 2.73 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.formatHijriDate = void 0; // formatHijriDate.ts const luxon_1 = require("luxon"); const hMonths_1 = require("./hMonths"); const hWeekdays_1 = require("./hWeekdays"); /** * Formats a Hijri date according to the given format string * @param {Date} hijriDate - The Hijri date to format * @param {string} format - The format string * @returns {string} - The formatted date string */ function formatHijriDate(hijriDate, format) { // Replace each pattern in the format string with the corresponding value return format.replace(/\biYYYY\b|\biYY\b|\biMM\b|\biM\b|\biMMM\b|\biMMMM\b|\biDD\b|\biD\b|\biE\b|\biEEE\b|\biEEEE\b|\b[HHhmsaiozZ]+\b/g, (match) => { switch (match) { case 'iYYYY': return String(hijriDate.hy).padStart(4, '0'); case 'iYY': return String(hijriDate.hy % 100).padStart(2, '0'); case 'iMM': return String(hijriDate.hm).padStart(2, '0'); case 'iM': return String(hijriDate.hm); case 'iMMM': return hMonths_1.hmMedium[hijriDate.hm - 1]; case 'iMMMM': return hMonths_1.hmLong[hijriDate.hm - 1]; case 'iDD': return String(hijriDate.hd).padStart(2, '0'); case 'iD': return String(hijriDate.hd); case 'iE': return String(hWeekdays_1.hwNumeric[luxon_1.DateTime.fromObject({ year: hijriDate.hy, month: hijriDate.hm, day: hijriDate.hd }).weekday - 1]); case 'iEEE': return hWeekdays_1.hwShort[luxon_1.DateTime.fromObject({ year: hijriDate.hy, month: hijriDate.hm, day: hijriDate.hd }).weekday - 1]; case 'iEEEE': return hWeekdays_1.hwLong[luxon_1.DateTime.fromObject({ year: hijriDate.hy, month: hijriDate.hm, day: hijriDate.hd }).weekday - 1]; // The following patterns are the same for both Gregorian and Hijri case 'HH': case 'H': case 'hh': case 'h': case 'mm': case 'm': case 'ss': case 's': case 'a': case 'iooo': case 'ioooo': case 'z': case 'zz': case 'Z': // Use Luxon's DateTime formatting for these patterns const gregorianDate = luxon_1.DateTime.fromObject({ year: hijriDate.hy, month: hijriDate.hm, day: hijriDate.hd }); return gregorianDate.toFormat(match); default: return match; } }); } exports.formatHijriDate = formatHijriDate;