Tuesday, March 01, 2005

C#: Hebrew Date

public static string GetHebrewJewishDateString(DateTime anyDate, bool addDayOfWeek)
{
System.Text.StringBuilder hebrewFormatedString = new System.Text.StringBuilder();

// Create the hebrew culture to use hebrew (Jewish) calendar
CultureInfo jewishCulture = CultureInfo.CreateSpecificCulture("he-IL");
jewishCulture.DateTimeFormat.Calendar = new HebrewCalendar();

#region Format the date into a Jewish format

if (addDayOfWeek)
{
// Day of the week in the format " "
hebrewFormatedString.Append(anyDate.ToString("dddd", jewishCulture) + " ");
}

// Day of the month in the format "'"
hebrewFormatedString.Append(anyDate.ToString("dd", jewishCulture) + " ");

// Month and year in the format " "
hebrewFormatedString.Append("" + anyDate.ToString("y", jewishCulture));

#endregion

return hebrewFormatedString.ToString();
}

5 comments:

Anonymous said...

Thanks
Eliezer, Israel
www.koltuv.org לזיכוי הרבים בספרי קודש

givolaj said...

thank you very much!!!!
תודה רבה!

WebStar said...

A grat and genius solution!
Thanks!
HaVer

Shimmy said...

Replace your code with:


public static string ToJewishDateString(this DateTime value, string format)
{
var ci = CultureInfo.CreateSpecificCulture("he-IL");
ci.DateTimeFormat.Calendar = new HebrewCalendar();
return value.ToString(format, ci);
}
public static string ToJewishDateString(this DateTime value, bool dayOfWeek)
{
var format = dayOfWeek ? "D" : "d";
return value.ToJewishDateString(format);
}

To be more string-format flexible.

Anonymous said...

תודה רבה!
Thank you very much!!! :)