Skip to content

Instantly share code, notes, and snippets.

@jafar-jabr
Last active July 29, 2022 23:18
Show Gist options
  • Save jafar-jabr/ad6014fc80c9bd31cc8e8c197b3d453b to your computer and use it in GitHub Desktop.
Save jafar-jabr/ad6014fc80c9bd31cc8e8c197b3d453b to your computer and use it in GitHub Desktop.
Convert Hijri date to Gregorian
const HijriToGregorian = (hijriDate) => {
const handelAmericanFormat = (_date) => {
const [month, day, year] = _date.split('/');
return new Date(Date.UTC(year, month, day));
};
const addDays = (_date, days) => {
let date;
if (typeof _date === 'string') {
const [month, day, year] = _date.split('/');
date = new Date(Date.UTC(year, month, day));
} else {
date = new Date(_date.getTime());
}
date.setDate(date.getDate() + days);
return date;
};
const minusMonth = (_date) => {
const date = new Date(_date.getTime());
date.setMonth(date.getMonth() - 1);
return date;
};
const getDaysBetween = (startTime, endTime) => {
const startTimestamp = startTime.valueOf();
const endTimestamp = endTime.valueOf();
const _diff = (endTimestamp - startTimestamp)/1000;
const totalHours = Math.floor(_diff/3600);
return Math.floor(totalHours/24);
}
const date = handelAmericanFormat(hijriDate);
const [gregorianV1] = Intl.DateTimeFormat('en-IQ-u-ca-islamic').format(date).split(' ');
const diff = getDaysBetween(handelAmericanFormat(gregorianV1), handelAmericanFormat(hijriDate));
const factor = 0.97; // experimental
let possibleDate = addDays(hijriDate, diff * factor);
let [refDate] = Intl.DateTimeFormat('en-IQ-u-ca-islamic').format(possibleDate).split(' ');
if (hijriDate === refDate) {
return possibleDate;
}
let index = 0;
while (index < 10000) {
possibleDate = addDays(possibleDate, 1);
[refDate] = Intl.DateTimeFormat('en-IQ-u-ca-islamic').format(possibleDate).split(' ');
index += 1;
if (refDate === hijriDate) {
break;
}
}
return Intl.DateTimeFormat('UTC', {dateStyle: 'medium'} ).format(minusMonth(possibleDate));
};
@albalawi14
Copy link

السلام عليكم هل يمكن المساعدة بمقطع فيديو
وشكرا

@jafar-jabr
Copy link
Author

عليكم السلام
مافهمتك

@albalawi14
Copy link

عندي ملف اكسل على google sheet يوجد به تواريخ هجرية ابي احوله الى ميلادي ووكذلك العكس من هجري الى ميلادي
and thanks

@jafar-jabr
Copy link
Author

خي بالاول تقرا ملف الاكسل
بعدين تغير التواريخ باستخدام هاي الفنكشن او غيرها
بعدين ترجع تكتب البيانات للاكسل

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