Overview
The Islamic utilities provide functions for working with the Hijri calendar, including formatting dates and accessing Islamic month names.
Formats a Hijri date object into a human-readable string.
function formatHijriDate(hijriDate: {
day: number;
month: number;
year: number;
}): string
Parameters
The Hijri date object to format.The day of the month (1-30).
The month number (1-12), where 1 is Muharram and 12 is Dhul-Hijjah.
The Hijri year (e.g., 1445).
Returns
A formatted Hijri date string in the format: “[day] [month name] [year] AH”
Example
import { formatHijriDate } from '@/lib/utils/islamic';
// Format a Hijri date
const hijriDate = {
day: 15,
month: 9,
year: 1445
};
const formatted = formatHijriDate(hijriDate);
console.log(formatted);
// Output: "15 Ramadan 1445 AH"
// Format the first day of Muharram
const newYear = formatHijriDate({ day: 1, month: 1, year: 1446 });
console.log(newYear);
// Output: "1 Muharram 1446 AH"
hijriMonths
An array containing the names of all Islamic months in order.
const hijriMonths: string[]
Values
The array contains the following month names:
- Muharram
- Safar
- Rabi al-Awwal
- Rabi al-Thani
- Jumada al-Awwal
- Jumada al-Thani
- Rajab
- Shaban
- Ramadan
- Shawwal
- Dhul-Qadah
- Dhul-Hijjah
Example
import { hijriMonths } from '@/lib/utils/islamic';
// Get the name of a specific month
const monthNumber = 9;
const monthName = hijriMonths[monthNumber - 1];
console.log(monthName);
// Output: "Ramadan"
// Display all months
hijriMonths.forEach((month, index) => {
console.log(`${index + 1}. ${month}`);
});
parseTimeString
Parses a time string into a Date object for a given base date. This utility supports both 12-hour (AM/PM) and 24-hour time formats.
function parseTimeString(timeString: string, baseDate: Date): Date
Parameters
The time string to parse. Supports formats like:
- “5:30 AM”
- “12:00 PM”
- “17:45”
- “5:30”
The Date object to use for the year, month, and day components.
Returns
A new Date object with the parsed time, using the year, month, and day from baseDate.
Example
import { parseTimeString } from '@/lib/utils/time';
const today = new Date('2024-03-15');
// Parse 12-hour format
const fajrTime = parseTimeString('5:30 AM', today);
console.log(fajrTime);
// Output: 2024-03-15T05:30:00.000Z
// Parse 24-hour format
const ishaTime = parseTimeString('19:45', today);
console.log(ishaTime);
// Output: 2024-03-15T19:45:00.000Z
// Handle noon edge case
const noonTime = parseTimeString('12:00 PM', today);
console.log(noonTime.getHours());
// Output: 12
// Handle midnight edge case
const midnightTime = parseTimeString('12:00 AM', today);
console.log(midnightTime.getHours());
// Output: 0