2

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);
ASh
  • 34,632
  • 9
  • 60
  • 82
irena
  • 61
  • 6
  • Working with Dates is a pain. The kind that can drive people borderline insane: https://www.youtube.com/watch?v=-5wpm-gesOY | That being said, for dates after the start of the Unix Epoch (1.1.1970), .NET has *exceptional* support. Usually stuff like Callendar, Culture and Formats are extracted from Windows. But of course you can override it. I actually found this little example: https://stackoverflow.com/a/19075881/3346583 – Christopher Feb 01 '20 at 23:16
  • The reason for the limitation is, that internally all taht DateTime saves is the number of Ticks since start of the Unix Epoch. Every other property - including the string representation - are just a interpretation of that value. For dates before the Unix Epoch, it becomes Historian Hard. – Christopher Feb 01 '20 at 23:18
  • Would you be ok with correcting the spelling of `Bearth` to `Birth` within your code, so it's not so distracting? – Wyck Feb 01 '20 at 23:46
  • Apparently people hate me giving you relevant background infroamtion, so I have to delete my answer. – Christopher Feb 02 '20 at 20:18

2 Answers2

2

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.

yaakov
  • 5,552
  • 35
  • 48
1

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.

lax9999
  • 103
  • 6
  • Try something like: `var dob = DateTime.Parse("[Gregorian DateTime]", CultureInfo.InvariantCulture); var hc = new HebrewCalendar(); var hebrewDate = new DateTime(hc.GetYear(dob), hc.GetMonth(dob), hc.GetDayOfMonth(dob));` – Jimi Feb 01 '20 at 23:18
  • @Jimi I've tried your code, but it doesn't answer the initial question (hebrew -> gregorian) and it doesn't fix problems (as far as I see) in my answer. Tell me if I'm wrong (explaining what or where I'm wrong and possibly a suggestion to solve). – lax9999 Feb 02 '20 at 00:05
  • That's for the Hebrew DateTime, now you just create a new DateTime from it ~in the same way: `var gregorianDate = new DateTime(hebrewDate.Year, hebrewDate.Month, hebrewDate.Day, hc);` (`hc` is the Hebrew calendar created in the previous comment :). So you have both ways. – Jimi Feb 02 '20 at 00:14