irena
irena

Reputation: 61

C# convert Hebrew calendar date to Gregorian calendar date

I would like to convert Hebrew dates (Jewish) to Gregorian date; how can I do it? I tried this, but it's not working:

string birth = txtBearthDay.Text+" "+ txtBearthMonth.Text+" " + txtBearthYear.Text;
HebrewCalendar hc = new HebrewCalendar();
CultureInfo jewishCulture = CultureInfo.CreateSpecificCulture("he-IL");

jewishCulture.DateTimeFormat.Calendar = hc;
dt = DateTime.Parse(birth,jewishCulture.DateTimeFormat);

Upvotes: 2

Views: 1592

Answers (2)

yaakov
yaakov

Reputation: 5850

It's actually surprisingly simple:

new DateTime(5780, 5, 7, new HebrewCalendar())

This will return a DateTime object with Gregorian property values (2020-02-02), as the properties of a DateTime are rendered with the Gregorian calendar. If you want to get the Hebrew calendar values back out of the DateTime, you must explicitly query the HebrewCalendar class.

Upvotes: 4

lax9999
lax9999

Reputation: 103

Try string date_in_oth_culture = dt.ToString(new CultureInfo("he-IL");. I hope it helps.


Edit: Sorry, I misunderstood what you mean. You can find more informations about how to use HebrewCalendar here.
Edit 2: Unfortunately neither the code on the documentation seems working and on the internet I couldn't find anything.

A solution might be useing HebrewCalendar to calculate new dates and then use a switch statement to translate them yourself.

HebrewCalendar hc = new HebrewCalendar();
CultureInfo culture = CultureInfo.CreateSpecificCulture("he-IL");
culture.DateTimeFormat.Calendar = hc;

DateTime hebrDate = hc.ToDateTime(year, month, day, hour, minute, second, millisecond, era); // Converts Hebrew date into Gregorian date


DateTime gregDate = new DateTime(2020, 2, 1); // Gregorian date

string hebrMonth;
switch(hc.GetMonth(gregDate))
{
  case 1:
    hebrMonth = "___";
    break;
  case 2:
    hebrMonth = "___";
    break;
  // And so on
}

string finalDate = hc.getDayOfMonth(gregDate) + " " + hebrMonth + " " + hc.getYear(gregDate) + " " + hc.getEra(gregDate);

I hope it helps.

Upvotes: 1

Related Questions