is there any way to get first and last date of current Hijri month with JavaScript?
is there any way to get first and last date of current Hijri month with JavaScript?
The following function will return information about the Hijri Date as 7 elements in an array given a Gregorian date. This can be the current date or any other date.
The information returned by the function is:
[0] The Gregorian Date (the input date) in YYYY-MM-DD format
[1] The Hijri Day (Number) (1-30)
[2] The Hijri Month (String)
[3] The Hijri Year (Number)
[4] Total Days in the Hijri Month (Number) (29 or 30)
[5] The Gregorian Date that the Hijri Month starts on (Date object)
[6] The Gregorian Date that the Hijri Month ends on (Date object)
The test case examples provide the information on today's Hijri Date (and month) and another date.
/*********************************************************************
* @function : getHijriMonthInfo(date)
* @purpose : Returns information on a Hijri Month for a given Gregorian Date
* @version : 1.00
* @author : Mohsen Alyafei
* @date : 06 Feb 2022
* @Licence : MIT
* @param : {date} a valid Date Object
* @returns : An Array of seven (7) elements with the following data:
* [0] The Gregorian Date (the input date) in YYYY-MM-DD format
* [1] The Hijri Day (Number) (1-30)
* [2] The Hijri Month (String)
* [3] The Hijri Year (Number)
* [4] Total Days in the Hijri Month (Number) (29 or 30)
* [5] The Gregorian Date that the Hijri Month starts on (Date object)
* [6] The Gregorian Date that the Hijri Month ends on (Date object)
**********************************************************************/
function getHijriMonthInfo(date) {
let startDate= new Date(date),
c= 'en-u-ca-islamic-umalqura-nu-latn', // use 'islamic-umalqura' calendar for the islamic date
d= startDate, gDays=0, iMonthTotalDays= 0, i_Day, n='numeric',
iDay =new Intl.DateTimeFormat(c,{day :n}).format(startDate),
iMonth=new Intl.DateTimeFormat(c,{month:'long'}).format(startDate),
iYear =new Intl.DateTimeFormat(c,{year :n}).format(startDate).split(" ")[0];
for (let i=0; i<32; i++) {
i_Day= new Intl.DateTimeFormat(c,{day:n}).format(d); // Hijri day
if (+i_Day>iMonthTotalDays) iMonthTotalDays= i_Day, gDays++;
else break;
d= new Date(d.setDate(d.getDate()+1)); // next Gregorian day
}
let gEndT = new Date(startDate.setDate(startDate.getDate()+gDays-2)),
gEnd = new Date(gEndT),
gStart= new Date(gEndT.setDate(gEndT.getDate()-iMonthTotalDays+1));
return [new Date(date).toISOString().split("T")[0],+iDay,iMonth,+iYear,iMonthTotalDays,gStart,gEnd];
}
//==================================================================
// Test Cases
//==================================================================
output(getHijriMonthInfo(new Date(Date.now()))); // today's date
output(getHijriMonthInfo("2022-04-22")); // 22 April 2022
//==================
function output([gDate, iDay, iMonth, iYear, iMonthTotalDays, gStart, gEnd]) {
console.log(`
Gregorian Date : ${gDate}
Hijri Day : ${iDay}
Hijri Month : ${iMonth}
Hijri Year : ${iYear}
Total Days in Hijri Month : ${iMonthTotalDays}
Hijri Month Starts on : ${gStart}
Hijri Month Ends on : ${gEnd}
`
);
}
//==================
The alternative to getting the last day of the current Hijri month is to use the ECMAScript proposal "Temporal" which is currently in Stage 3 of Active Proposals.
<script type='module'>
// ====== load Temp polyfill (not needed after implementation) ========
import * as TemporalModule from 'https://cdn.jsdelivr.net/npm/@js-temporal/polyfill@0.3.0/dist/index.umd.js'
//=====================================================================
const Temporal = temporal.Temporal;
// Note using 'islamic-umalqura' Calendar
let daysIncurrentHijriMonth = Temporal.PlainDate.from( Temporal.Now.plainDateISO()).withCalendar('islamic-umalqura').daysInMonth;
console.log("Current Hijri Month is: ", daysIncurrentHijriMonth," days");
</script>
have you tryied using
new Intl.DateTimeFormat('ar-TN-u-ca-islamic', {day: 'numeric', month: 'long',weekday: 'long',year : 'numeric'}).format(Date.now());
This returns the current hijri date, you may need to do some calculations to get first and last