Skip to content

Instantly share code, notes, and snippets.

@irfanbaigse
Created October 24, 2019 12:40
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save irfanbaigse/41b0f5054dcc50e62b5da6e5b4be9e37 to your computer and use it in GitHub Desktop.
Save irfanbaigse/41b0f5054dcc50e62b5da6e5b4be9e37 to your computer and use it in GitHub Desktop.
convert Gregorian to Hijri date in javascript
import { gregorianToJulian, julianToHijri } from './hijri-util-date.js';
/**
* Gregorian to Hijri
* * First convert to Julian
* * then convert to hijri
*/
const futureDate = dayjs('2019-10-24').add(1, 'year');
// .format('YYYY-MM-DD');
const y = futureDate.year();
const d = futureDate.day();
const m = futureDate.month();
const julianDay = gregorianToJulian(y, m, d);
const { year, month, day } = julianToHijri(julianDay);
console.log(futureDate.format('YYYY-MM-DD'), julianDay, year, month, day);
// Refer original code
// https://github.com/OpenSID/OpenSID/blob/master/donjo-app/libraries/Date_conv.php#L44
const hijriToJulian = (year, month, day) => {
return (
Math.floor((11 * year + 3) / 30) +
Math.floor(354 * year) +
Math.floor(30 * month) -
Math.floor((month - 1) / 2) +
day +
1948440 -
386
);
};
const gregorianToJulian = (year, month, day) => {
if (month < 3) {
year -= 1;
month += 12;
}
const a = Math.floor(year / 100.0);
const b = year === 1582 && (month > 10 || (month === 10 && day > 4))
? -10 :
year === 1582 && month === 10
? 0 :
year < 1583
? 0 :
2 - a + Math.floor(a / 4.0);
return Math.floor(365.25 * (year + 4716)) + Math.floor(30.6001 * (month + 1)) + day + b - 1524;
};
const julianToHijri = (julianDay) => {
const y = 10631.0 / 30.0;
const epochAstro = 1948084;
const shift1 = 8.01 / 60.0;
let z = julianDay - epochAstro;
const cyc = Math.floor(z / 10631.0);
z -= 10631 * cyc;
const j = Math.floor((z - shift1) / y);
z -= Math.floor(j * y + shift1);
const year = 30 * cyc + j;
let month = Math.floor(parseInt((z + 28.5001) / 29.5));
if (month === 13) {
month = 12;
}
const day = z - Math.floor(29.5001 * month - 29);
return { year: parseInt(year), month: parseInt(month), day: parseInt(day) };
};
const julianToGregorian = (julianDate) => {
let b = 0;
if (julianDate > 2299160) {
const a = Math.floor((julianDate - 1867216.25) / 36524.25);
b = 1 + a - Math.floor(a / 4.0);
}
const bb = julianDate + b + 1524;
let cc = Math.floor((bb - 122.1) / 365.25);
const dd = Math.floor(365.25 * cc);
const ee = Math.floor((bb - dd) / 30.6001);
const day = bb - dd - Math.floor(30.6001 * ee);
let month = ee - 1;
if (ee > 13) {
cc += 1;
month = ee - 13;
}
const year = cc - 4716;
return { year: parseInt(year), month: parseInt(month), day: parseInt(day) };
};
export { hijriToJulian, gregorianToJulian, julianToHijri, julianToGregorian };
@albalawi14
Copy link

Peace be upon you can you help with the application on google sheet with a video
and thanks

@mhsfh
Copy link

mhsfh commented Oct 12, 2022

Great job 💯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment