0

I am trying to convert HijriCalendar to Gregorian Calendar in Xamarin Forms app. However, it seems that Xamarin Android is not happy with it. I haven't test it in Xamarin iOS. I am using code from Cannot convert from Hijri Date to Gregorian date (c#) and I am getting below exception when instantiating the HijriCalendar object in line

DTFormat.Calendar = new System.Globalization.HijriCalendar(); //exception is thrown

{System.ArgumentOutOfRangeException: Not a valid calendar for the given culture. Parameter name: value at System.Globalization.DateTimeFormatInfo.set_Calendar (System.Globalization.Calendar value) [0x00142]

I have checked Converting Dates between Calendars in Xamarin.Android solution, but that suggests another library which I am not interested to use. I have changed the Linking properties in Android Project to None, Sdk Assemblyies Only, and Sdk & User Assembly but didn't worked. So, how to convert Gregorian Date to HijriDate in Xamarin forms?

I also added the following codes in OnCreate method in Xamarin Android from Thai crash: Not a valid calendar for the given culture

_ = new System.Globalization.GregorianCalendar();
_ = new System.Globalization.HijriCalendar();
_ = new System.Globalization.PersianCalendar();
_ = new System.Globalization.UmAlQuraCalendar();
_ = new System.Globalization.ThaiBuddhistCalendar();
ARH
  • 1,566
  • 3
  • 25
  • 56
  • https://forums.xamarin.com/discussion/128789/how-to-display-gregorian-date-in-datepicker-instead-of-hijri-when-culture-is-arabic – Russ J Nov 30 '20 at 23:40

2 Answers2

0

I found simular problem on this link https://github.com/xamarin/Xamarin.Forms/issues/4037

// These classes won't be linked away because of the code,
// but we also won't have to construct unnecessarily either,
// hence the if statement with (hopefully) impossible
// runtime condition.
//
// This is to resolve crash at CultureInfo.CurrentCulture
// when language is set to Thai. See
// https://github.com/xamarin/Xamarin.Forms/issues/4037
if (Environment.CurrentDirectory == "_never_POSSIBLE_")
{
    new System.Globalization.ChineseLunisolarCalendar();
    new System.Globalization.HebrewCalendar();
    new System.Globalization.HijriCalendar();
    new System.Globalization.JapaneseCalendar();
    new System.Globalization.JapaneseLunisolarCalendar();
    new System.Globalization.KoreanCalendar();
    new System.Globalization.KoreanLunisolarCalendar();
    new System.Globalization.PersianCalendar();
    new System.Globalization.TaiwanCalendar();
    new System.Globalization.TaiwanLunisolarCalendar();
    new System.Globalization.ThaiBuddhistCalendar();
    new System.Globalization.UmAlQuraCalendar();
}

Good luck!

WhiteBite
  • 535
  • 2
  • 12
  • I have followed that link and https://github.com/xamarin/Xamarin.Forms/issues/4037 (Added those languages OnCreate method in xamarin Android, but didn't worked) – ARH Nov 30 '20 at 23:51
0

Try to use System.DateTime:

Calendar hijiri = new HijriCalendar();
DateTime date = new DateTime(1442, 4, 15, hijiri);
Console.WriteLine(date.Year); // 2020
Console.WriteLine(date.Month);//11
Console.WriteLine(date.Day); // 30


Calendar gregorian = new GregorianCalendar();
DateTime d = new DateTime(2020, 11, 30, gregorian);
var year = hijiri.GetYear(d);  //1442
var month = hijiri.GetMonth(d);  //4
var day = hijiri.GetDayOfMonth(d);  //15

and another way is using NodaTime.

Leo Zhu
  • 15,726
  • 1
  • 7
  • 23
  • This works but the output seems wrong to me. The output should be 1442,4,15 accordingly for year,month, and day. Please refer to https://www.islamicity.org/hijri-gregorian-converter/ – ARH Dec 01 '20 at 17:23
  • Your answer and the guide in this link help me out to convert HijriCalendar to Gregorian and vice versa. – ARH Dec 01 '20 at 19:10
  • @ARH It's my mistake.I'm just illustrating one way, not using the correct date, you can refer to the update above.If it is helpful to you,could you mark it as an acceptable answer. – Leo Zhu Dec 02 '20 at 01:37