How to get the English name of the month from the Jewish calendar in C #

I am trying to print the Hebrew date in English with C #. The following gives a Hebrew date:

var ci = System.Globalization.CultureInfo.CreateSpecificCulture("he-IL"); ci.DateTimeFormat.Calendar = new System.Globalization.HebrewCalendar(); Response.Write(DateTime.Today.ToString("MMM d, yyyy", ci)); Response.Write(DateTime.Today.ToString("dMy", ci)); 

gives

 ื›ืกืœื• ื›"ื•, ืชืฉืข"ื” ื›"ื•-ื’'-ืชืฉืข"ื” 

December 18, 2014. Changing CultureInfo to "en-US" causes "Invalid calendar for this culture." error. I'm trying to get

 26 Kislev 5775 

and

 26-09-5775 
+5
source share
5 answers

I could not figure out how to set up an array of month names for leap years or an array of day numbers so that they appear as English numbers, not Hebrew letters. My solution was:

Globals.cs

  public static string[] HebrewMonthNames = { "Tishrei", "Cheshvan", "Kislev", "Tevet", "Shevat", "Adar", "Nissan", "Iyar", "Sivan", "Tamuz", "Av", "Elul" }; public static string[] HebrewMonthNamesLeapYear = { "Tishrei", "Cheshvan", "Kislev", "Tevet", "Shevat", "Adar I", "Adar II", "Nissan", "Iyar", "Sivan", "Tamuz", "Av", "Elul" }; 

Utils.cs

 public string FormatHebrewDate(DateTime dtGregorian) { System.Globalization.HebrewCalendar hCal = new System.Globalization.HebrewCalendar(); string sDate = hCal.GetDayOfMonth(dtGregorian).ToString() + " "; if (hCal.IsLeapYear(hCal.GetYear(dtGregorian))) { sDate += Globals.HebrewMonthNamesLeapYear[hCal.GetMonth(dtGregorian) - 1]; } else { sDate += Globals.HebrewMonthNames[hCal.GetMonth(dtGregorian) - 1]; } sDate += " " + hCal.GetYear(dtGregorian).ToString(); return sDate; } 
+3
source

Option 1:

You can override DateTimeFormatInfo.MonthNames and MonthGenitiveNames , as well as their respective AbbreviatedMonthNames and AbbreviatedMonthGenitiveNames .

These are simple 1-dimensional arrays of string[] and have public setters , which allows you to add your custom translations to CultureInfo:

When this property is set, the array must be one-dimensional and must have exactly 13 elements. Calendar objects contain calendars from 13 months. The first element (an element with a zero index) represents the first month of the year defined by the Calendar property.

If you set the MonthNames property, you must also set the MonthGenitiveNames property.

If the user template contains an MMMM format template, DateTime.ToString displays the MonthNames value instead of MMMM in the format template.

This property affects if the value of the Calendar property changes.

So you can change the sample code as follows:

 // I am just using German Number representations for the example. // Use additional string Arrays to suit the abbrevated // and the Genetive names. // Replaye with whatever suits your needs. string[] monthNames = { "Eins", "Zwei", "Drei", "Vier", "Fรผnf", "Sechs", "Sieben", "Acht", "Neun", "Zehn", "Elf", "Zwรถlf", string.Empty }; // Assign each string Array to its corresponding property. // I am using the same Array here just as an example for // what is possible and because I am lazy... :-) ci.DateTimeFormat.MonthNames = monthNames; ci.DateTimeFormat.MonthGenitiveNames = monthNames; ci.DateTimeFormat.AbbreviatedMonthNames = monthNames; ci.DateTimeFormat.AbbreviatedMonthGenitiveNames = monthNames; 

These names will be used along with your output format string, just as you want.

Each time you change the calendar, these overrides will be lost. Therefore, you need to make sure that you reassign custom values โ€‹โ€‹if you need it.

[Update] Option 2:

A more permanent approach might be to use the CultureAndRegionInfoBuilder class.

Defines a custom culture that is new or based on a different culture and country / region. A user culture can be installed on a computer and subsequently used by any application running on that computer.

You can either create a complete replacement for the "he-IL" culture version, or create a variant using only your custom translations or something in between.

Using this approach, you do not need to manually ensure that translations are performed after each Culture switch in the application, as in Option 1. After registering a new user culture, you can use it like any other CultureInfo.

Please note that your application will need administrative privileges to register a new user culture.

Creating a custom culture is not too complicated, as shown in the following code snippet.

Example from MSDN: CultureAndRegionInfoBuilder

The following example defines the user culture ru-US, representing Russian in the United States. In this example, the user culture, loading settings from the Russian (Russia) CultureInfo object and RegionInfo object, and then sets the number of CultureAndRegionInfoBuilder properties. An example registers a user culture, and then creates it and the current thread culture does.

 using System; using System.Globalization; using System.Threading; public class Example { public static void Main() { // Create a custom culture for ru-US. CultureAndRegionInfoBuilder car1 = new CultureAndRegionInfoBuilder("ru-US", CultureAndRegionModifiers.None); car1.LoadDataFromCultureInfo(CultureInfo.CreateSpecificCulture("ru-RU")); car1.LoadDataFromRegionInfo(new RegionInfo("en-US")); car1.CultureEnglishName = "Russian (United States)"; car1.CultureNativeName = " ()"; car1.CurrencyNativeName = " ()"; car1.RegionNativeName = ""; // Register the culture. try { car1.Register(); } catch (InvalidOperationException) { // Swallow the exception: the culture already is registered. } // Use the custom culture. CultureInfo ci = CultureInfo.CreateSpecificCulture("ru-US"); Thread.CurrentThread.CurrentCulture = ci; Console.WriteLine("Current Culture: {0}", Thread.CurrentThread.CurrentCulture.Name); Console.WriteLine("Writing System: {0}", Thread.CurrentThread.CurrentCulture.TextInfo); } } // The example displays the following output: // Current Culture: ru-US // Writing System: TextInfo - ru-US 
+1
source

I know this is not an ideal answer, but you can manually enter a list of Hebrew months and use DateTime.Today.Month as an index on this list. Similarly, DateTime.Today.Day and .Year provide integer output that you can use. Sorry, it seems a bit wrong to collapse your own formatting, right?

You can still use string.format() to make sure that it looks the way you want.

0
source

You can use this (I know this is not C #, but you can get what I'm doing here):

  Dim c As New CultureInfo("he-IL") c.DateTimeFormat.Calendar.ToDateTime(Now.Year, Now.Month, Now.Day, Now.Hour, Now.Minute, Now.Second, Now.Millisecond).ToString("MMMM", New CultureInfo("en-GB")) MessageBox.Show(c.DateTimeFormat.Calendar.ToDateTime(Now.Year, Now.Month, Now.Day, Now.Hour, Now.Minute, Now.Second, Now.Millisecond).ToString("MMMM", New CultureInfo("en-GB"))) 

But it will give you the name of the Gregorian calendar (December)

Piojo's suggestion for creating a vocabulary object that contains an English version of the Hebrew name might work better

0
source

I do not think .NET has the culture information you want to use. However, you can create your own CultureInfo and change the DateTimeFormat to suit your needs:

 var cultureInfo = CultureInfo.CreateSpecificCulture("he-IL"); cultureInfo.DateTimeFormat.Calendar = new HebrewCalendar(); cultureInfo.DateTimeFormat.AbbreviatedMonthNames = new[] { "Translation of ืชืฉืจื™", "Translation of ื—ืฉื•ืŸ", // 11 more elements }; cultureInfo.DateTimeFormat.AbbreviatedMonthGenitiveNames = new[] { ... }; cultureInfo.DateTimeFormat.MonthNames = new[] { ... }; cultureInfo.DateTimeFormat.MonthGenitiveNames = new[] { ... }; 

(Sorry for the lack of proper translations, but I don't know Hebrew.)

Then you can use this CultureInfo just like in your question.

If necessary, you can also change the names of the days in the same way.

It is important that the calendar be changed before changing the various properties of the month name and date. The number of expected entries in the month name arrays changes when the calendar changes.

0
source

Source: https://habr.com/ru/post/1209430/


All Articles