Roy Osherove

View Original

Formatting Hebrew dates

Based on the amount of articles out there relating to hebrew dating (and I don't mean a singles bar!) there's always confusion on how exactly to translate a date into its full hebrew representation. Here's a simple method that does this, based on information which is easily available via this article.

C#
/// <summary>
  /// Returns the Juwish formatted date
  /// </summary>
  /// <param name="anyDate">Any valid date</param>
  /// <param name="addDayOfWeek">Set this option to add the day of the week to the formatted date</param>
  /// <returns>A string containing the Juwish formatted date</returns>
  public static string GetHebrewJuwishDateString(DateTime anyDate, bool addDayOfWeek)
  {
   System.Text.StringBuilder hebrewFormatedString = new System.Text.StringBuilder();
   
   // Create the hebrew culture to use hebrew (Juwish) calendar
   CultureInfo juwishCulture = CultureInfo.CreateSpecificCulture("he-IL");
   juwishCulture.DateTimeFormat.Calendar = new HebrewCalendar();
 
   #region Format the date into a Juwish format
 
   if (addDayOfWeek)
   {
    // Day of the week in the format " "
    hebrewFormatedString.Append(anyDate.ToString("dddd", juwishCulture) + " ");
   }
 
   // Day of the month in the format "'"
   hebrewFormatedString.Append(anyDate.ToString("dd", juwishCulture) + " ");
 
   // Month and year in the format " "
   hebrewFormatedString.Append("" + anyDate.ToString("y", juwishCulture));
 
   #endregion
 
   return hebrewFormatedString.ToString();
 
  }

VB.NET
''' <summary>
    ''' Summary description for JewishDateHelper.
    ''' This Class holds a single method that returns the Hebrew Jewish date format.
    ''' </summary>
    Public Class JewishDateHelper
 
        ''' <summary>
        ''' Returns the Juwish formatted date
        ''' </summary>
        ''' <param name="anyDate">Any valid date</param>
        ''' <param name="addDayOfWeek">Set this option to add the day of the week to the formatted date</param>
        ''' <returns>A string containing the Juwish formatted date</returns>
        Public Shared Function GetHebrewJuwishDateString(ByVal anyDate As DateTime, ByVal addDayOfWeek As Boolean) As String
            Dim hebrewFormatedString As System.Text.StringBuilder = New System.Text.StringBuilder
 
            '' Create the hebrew culture to use hebrew (Juwish) calendar
            Dim juwishCulture As CultureInfo = CultureInfo.CreateSpecificCulture("he-IL")
            juwishCulture.DateTimeFormat.Calendar = New HebrewCalendar
 
            If (addDayOfWeek) Then
                '' Day of the week in the format " "
                hebrewFormatedString.Append(anyDate.ToString("dddd", juwishCulture) + " ")
            End If
 
            '' Day of the month in the format "'"
            hebrewFormatedString.Append(anyDate.ToString("dd", juwishCulture) + " ")
 
            '' Month and year in the format " "
            hebrewFormatedString.Append("" + anyDate.ToString("y", juwishCulture))
 
            Return hebrewFormatedString.ToString()
 
        End Function
 
Thanks, Ido!