Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any correct converter for Hijri dates to Gregorian dates

Tags:

c#

hijri

I have work on many projects with date converts. for example, I work on the solar calendar and how to convert them to Gregorian dates and vice versa. The solar calendar (Persian calendar) is almost similar to the Gregorian date in terms of the number of days in a year[leap or not].
But I recently worked on a project with the lunar calendar. As I research on the Lunar calendar, I realized that there isn't any single logical method to convert the lunar system to solar(at least as far as I know).
Here are some references links that I researched on them:

  1. Cannot convert from Hijri Date to Gregorian date (c#)
  2. Convert date from Hijri Calendar to Gregorian Calendar and vise versa
  3. Hijri Date To gregorian using DateTime.Parse
  4. Convert Hijri date to Gregorian dat

As I followed the above links, training, and testing people presented algorithms, I noticed that none of them are absolutely correct.

Suffice it to say, just Convert "1441/02/30" [Safar 30th] to Gregorian date which every method that you want to try.

The following image is helpful for the aforementioned example. enter image description here

I put here my some test codes and fails:

CultureInfo arSA = new CultureInfo("ar-SA");
arSA.DateTimeFormat.Calendar = new HijriCalendar();
var dateValue = DateTime.ParseExact("1441/02/30", "yyyy/MM/dd", arSA);
return dateValue.ToString();

The error for above Code:

The DateTime represented by the string is not supported in calendar System.Globalization.HijriCalendar.

HijriCalendar hc = new HijriCalendar();
DateTime date = new DateTime(1441, 2, 30, hc);
return date.ToString();

The error for above Code:

"Day must be between 1 and 29 for month 2."

string hijri = "1441/2/30";
HijriCalendar hc = new HijriCalendar();
int year = int.Parse(hijri.Substring(0, 4));
string rem = hijri.Remove(0, 5);
int end = rem.IndexOf('/', 0);
int month = int.Parse(rem.Substring(0, end));
rem = rem.Remove(0, end + 1);
int day = int.Parse(rem);
DateTime date = new DateTime(year, month, day, hc);
return date.ToShortDateString();

The error for above Code:

"Day must be between 1 and 29 for month 2."

After that, I was trying to understand that is there any algorithm to deploying the convert Hijri date to Gregorian date?
So I test some Islamic online date converter and I got surprised!
Just try to enter "1441/2/30" on both date convertors.
the first one returning October 30, 2019
And the second one returning 29 October 2019
Hijri gregorian-converter
Islamic Date Converter - Gregorian Calendar Converter

So I don't know is there any real algorithm to convert Hijri dates to Gregorian .
Thanks in advance for your time.

For more info, https://www.wikiwand.com/simple/Islamic_calendar


Update: I know there isn't any correct library for know. but if someone has knowledge or details about Hijri Calendar (all its interface) please just describe here I really want to deploy a Hijri calendar for all.

like image 980
14 revs, 3 users 95% Avatar asked Jan 14 '20 11:01

14 revs, 3 users 95%


People also ask

How do I change my date from Hijri to Gregorian?

On your Android phone or tablet, open your Google app Google Search. Tap More ... > Settings > Language & region > Search region. Tap the region you want to see search results in.

Can Muslims use Gregorian calendar?

The Islamic calendar is the official calendar in countries around the Gulf, especially Saudi Arabia. But other Muslim countries use the Gregorian calendar for civil purposes and only turn to the Islamic calendar for religious purposes. What does an Islamic year look like?

Is the Hijri calendar accurate?

Therefore the Islamic calendar is a strict lunar calendar. Because the beginning of the Hijric month is determined by observation, it cannot be accurately predicted.


Video Answer


1 Answers

You can use something like this code to convert and parse hijri calendar strings to gregorian DateTime.

//assuming current culture is like en-us or something with gregorian calendar
string hijri = "1441/2/30";
HijriCalendar hc = new HijriCalendar();
var dateParts = hijri.Split('/');
DateTime? gregorianDate=null;
if (dateParts.Length == 3 && int.TryParse(dateParts[0], out int year) &&
     int.TryParse(dateParts[1], out int month) && int.TryParse(dateParts[2], out int day))
{
      if(month == 2 && day==30)
      {
         var temp = hc.ToDateTime(year, month, 29, 0, 0, 0, 0);
         gregorianDate = temp.AddDays(1);
      }
      else
      {
         gregorianDate = hc.ToDateTime(year, month, day, 0, 0, 0, 0);
      }

}
like image 73
Ali Hamidi Avatar answered Sep 18 '22 14:09

Ali Hamidi