Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi Everyone!
I have this code in c#:
C#
public static string GetHebrewJewishDateString(DateTime anyDate) 
{ 
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(); 

// 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)); 
 
return hebrewFormatedString.ToString(); 
}
the function get a gregorian DateTime and returns the equal Hebrew Date as a string.
I would like to do the opposite, means: to send a function a string of hebrew date and to get the equal Gregorian DateTime.

truly, I'm about to check if a specific Hebrew Date falls between two other Hebrew dates, so if someone have more effective idea (instead of converting the whole three dates to DateTime) - I'd be very glad to hear.

Thanks a lot !!
Tali.

[Edit: Added PRE tag]
Posted
Updated 31-May-11 1:48am
v2
Comments
Sergey Alexandrovich Kryukov 31-May-11 15:37pm    
Do you mean you want to get 27 or 28 Iyyar, 5771 for today?
--SA
Member 7928594 5-Jun-11 15:57pm    
I need to know if a part of hebrew date (day of month, month, year) is equal to Today, therefore I need a way to get today's Jewish month, today's Jewish year..
Do you know how can I get it ?

Ok, I see you converting a string. In my opinion this is not needed for your problem. Anyway here is a solution.
The DateTime.Parse and ToString methods provide everything you need.

Look at this example app:

using System;
using System.Globalization;

namespace DateTimeConverter
{
    class Program
    {
        static CultureInfo cultureJewish = CultureInfo.CreateSpecificCulture("he-IL");
        static void Main(string[] args)
        {
            cultureJewish.DateTimeFormat.Calendar = new HebrewCalendar();

            // create a hebrew date
            DateTime dtAnyDateHebrew = new DateTime(5767, 1, 1, new System.Globalization.HebrewCalendar());
            // write as hebrew date
            Console.WriteLine(dtAnyDateHebrew.ToString(cultureJewish));
            // write as current culture date
            Console.WriteLine(dtAnyDateHebrew.ToString());
            // convert date from hebrew datetime string (this is not realy needed, but you asked for it)
            DateTime dtConverted = ParseHebrewDateTimeString(dtAnyDateHebrew.ToString(cultureJewish));
            Console.WriteLine(dtConverted);
            
            // BTW: You can compare DateTimes like integers
            DateTime dtHebrewBegin = new DateTime(5767, 1, 1, new System.Globalization.HebrewCalendar());
            DateTime dtHebrewMiddle = new DateTime(5768, 1, 1, new System.Globalization.HebrewCalendar());
            DateTime dtHebrewEnd = new DateTime(5769, 1, 1, new System.Globalization.HebrewCalendar());
            // ...
            if (dtHebrewMiddle > dtHebrewBegin && dtHebrewMiddle < dtHebrewEnd)
                Console.WriteLine("Date {0} falls between {1} and {2}",
                    dtHebrewMiddle.ToString(cultureJewish),
                    dtHebrewBegin.ToString(cultureJewish),
                    dtHebrewEnd.ToString(cultureJewish));

            Console.ReadKey();
        }

        public static DateTime ParseHebrewDateTimeString(string strHebrew)
        {
            DateTime dtResult = DateTime.Parse(strHebrew, cultureJewish);
            return dtResult;
        }
    }
}
 
Share this answer
 
Comments
Member 7928594 5-Jun-11 5:16am    
So you might tell me a simple way to convert Today Hebrew date to a numeric Hebrew date?
means: a way to get today's hebrew month (Sivan) as a number (9), and today's hebrew month day as number?
It's not a problem to switch it, but its a problem to hadle leap years.
My purpose: to be able to send in the function you gave me:
DateTime dtAnyDateHebrew = new DateTime(today, today, 1, new System.Globalization.HebrewCalendar());
this in order to check if a specific day of month / month / year falls today.
Can you please help me?
johannesnestler 8-Jun-11 18:05pm    
You still need help? Sorry for late answer...
Very useful codes.
Thanks a lot.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900