How do you convert Gregorian dates to Islamic Hijri dates using JavaScript?
Converting Gregorian date to Hijri date
35.8k views Asked by FuNk AtThere are 5 answers
If you need only the year of Hijri date converted from Gregorian date (Miladi date) you can simply write this equation in javascript:
var GregorianYear = (new Date()).getFullYear();
var HijriYear = Math.round((GregorianYear - 622) * (33 / 32));
you can use this simple equation in the footer in master page like كل الحقوق محفوطة لـ ... ©
<script type="text/javascript">document.write((new Date()).getFullYear())</script> م - <script type="text/javascript">var y = (new Date()).getFullYear();var h = Math.round((y - 622) * (33 / 32));document.write(h)</script> هـ
you will get: كل الحقوق محفوطة لـ ... © 2014 م - 1435 هـ
you can also use C# embedded in asp page as:
<%= DateTime.Now.Year %> - <%= Math.Round((DateTime.Now.Year - 622) * 1.03125) %>
will return : 2014 - 1436
Finally, if you need to convert to UmAlQura date, simply try this line of code:
let _date = new Date('7/10/2019').toLocaleDateString('ar-SA').format('DD/MM/YYYY');
console.log(_date);
will return : ٧/١١/١٤٤٠ هـ
Update (2021-10-11) : If you need a more precise equation you have to know the current day of solar year by this formula :
var now = new Date();
var start = new Date(now.getFullYear(), 0, 0);
var diff = now - start;
var oneDay = 1000 * 60 * 60 * 24;
var dayOfYear = Math.floor(diff / oneDay);
Then you can get the Hijri year by this formula:
HijriYear = ((GregorianYear-621.5643)*365.24225 + dayOfYear) / 354.36707
Where :
365.24225 is the number of days in solar year.
354.36707 is the number of days in lunar year.
621.5643 is the exact Gregorian date of Hijra (start date of Hijri date)
HijriYear = ((2021-621.5643)*365.24225 + 284) / 354.36707 = 1,443.18444
So the current Hijri year is 1443 by using Math.floor(HijriYear)
function.
Also you can use the fraction :
var hijriDayOfYear = (HijriYear - Math.floor(HijriYear)) * 354.36707
0.18444 multiplied by 354.36707 to get the number of days in Hijri calendar :
0.18444 * 354.36707 = 65.3 : the number of day in this current Hijri year
Math.ceil(hijriDayOfYear / 29.530589) = 3
number of current Hijri month
Math.floor(hijriDayOfYear % 29.530589) = 6 ± 1
number of day in this Hijri month
Finally to summarize all of previous just use the following JS code:
var now = new Date()
var dayOfYear = Math.floor((new Date() - new Date(now.getFullYear(), 0, 0)) / (1000 * 60 * 60 * 24))
var hijriDate = ((now.getFullYear()-621.5643)*365.24225 + dayOfYear) / 354.36707
var hijriYear = Math.floor(hijriDate)
var hijriMonth = Math.ceil((hijriDate - Math.floor(hijriDate)) * 354.36707 / 29.530589)
var hijriDay = Math.floor((hijriDate - Math.floor(hijriDate)) * 354.36707 % 29.530589)
console.log(`${hijriYear}/${hijriMonth}/${hijriDay}`)
console output sample : 1443/3/6
General JavaScript Function to convert datetime to Hijri date:
function GetHijriDate(dateTime) {
var dayOfYear = Math.floor((dateTime - new Date(dateTime.getFullYear(), 0, 0)) / (1000 * 60 * 60 * 24))
var hijriDate = ((dateTime.getFullYear() - 621.5643) * 365.24225 + dayOfYear) / 354.36707
var hijriYear = Math.floor(hijriDate)
var hijriMonth = Math.ceil((hijriDate - Math.floor(hijriDate)) * 354.36707 / 29.530589)
var hijriDay = Math.floor((hijriDate - Math.floor(hijriDate)) * 354.36707 % 29.530589)
return [hijriYear, hijriMonth , hijriDay]
}
In C# :
/// <summary>
/// Gets the hijri date.
/// </summary>
/// <param name="date">The date.</param>
/// <returns></returns>
public static int[] GetHijriDate(DateTime date)
{
var yearOfHijra = 621.5643f;
var daysInSolarYear = 365.24225f;
var daysInLunarYear = 354.36707f;
var daysInLunarMonth = 29.53058f;
var hijriDate = ((date.Year - yearOfHijra) * daysInSolarYear + date.DayOfYear) / daysInLunarYear;
var hijriYear = (int)Math.Floor(hijriDate);
var hijriMonth = (int)Math.Ceiling((hijriDate - Math.Floor(hijriDate)) * daysInLunarYear / daysInLunarMonth);
var hijriDay = (int)Math.Floor((hijriDate - Math.Floor(hijriDate)) * daysInLunarYear % daysInLunarMonth);
int[] hijriDateRes = new int[3];
hijriDateRes[0] = hijriYear;
hijriDateRes[1] = hijriMonth;
hijriDateRes[2] = hijriDay;
return hijriDateRes;
}
Or Simply :
console.log(new Date().toLocaleDateString('ar-SA'))
'٥/٣/١٤٤٣ هـ'
Checkout my library hijrah-date which is a Javascript date in the Hijrah calendar system.
It also supports Hijrah to Gregorian and Gregorian to Hijrah conversion. In addition to date formatting.
The safest is to use the built-in javascript Intl.DateTimeFormat() constructor.
Here is an example of the 4 output formats in the Islamic Hijri Calendar of today's date.
Also examples of extracting the year and months separately under different formats.
let myFormat = 'en-u-ca-islamic-umalqura-nu-latn'; // use islamic-umalqura calendar (most modern)
let myDate = new Date(Date.now()); // today's date
let output = new Intl.DateTimeFormat(myFormat,{dateStyle:'full'}).format(myDate);
console.log("Full format : "+output);
output = new Intl.DateTimeFormat(myFormat,{dateStyle:'long'}).format(myDate);
console.log("Long format : "+output);
output = new Intl.DateTimeFormat(myFormat,{dateStyle:'medium'}).format(myDate);
console.log("Medium format : "+output);
output = new Intl.DateTimeFormat(myFormat,{dateStyle:'short'}).format(myDate);
console.log("Short format (m/d/yyyy): "+output);
console.log("=".repeat(50));
let yearFull = new Intl.DateTimeFormat(myFormat,{year:'numeric'}).format(myDate);
console.log("The full year : "+yearFull);
console.log("The year number : " +(+yearFull.split(" ")[0]));
let monthLong = new Intl.DateTimeFormat(myFormat,{month:'long'}).format(myDate);
let monthShort = new Intl.DateTimeFormat(myFormat,{month:'short'}).format(myDate);
let monthNum = new Intl.DateTimeFormat(myFormat,{month:'numeric'}).format(myDate);
let month2 = new Intl.DateTimeFormat(myFormat,{month:'2-digit'}).format(myDate);
console.log("The long month : "+monthLong);
console.log("The short month : "+monthShort);
console.log("The month number : "+monthNum);
console.log("The month 2 digits: "+month2);
In Javascript to convert the date, you could use Intl
(read more) as following:
a = new Date();
localeFormat= 'ar-SA-islamic-umalqura';
Intl.DateTimeFormat(localeFormat).format(a)
This converts current computer date to hijri. And with a little modification you can achieve that this snippet change any date to islamic
Taken from This site