Hijri and Gregorian DateTime constructor

10,584

Your first example is correct. The DateTime will not be in the Hijri format, it will just be the standardised equivalent of what you gave it. See the following code for how to get the Hirji date:

HijriCalendar hijri = new HijriCalendar();
DateTime firstDayInMonth = new DateTime(1433, 10, 11, hijri);
Console.WriteLine(hijri.GetEra(firstDayInMonth)); // 1
Console.WriteLine(hijri.GetYear(firstDayInMonth)); // 1433
Console.WriteLine(hijri.GetMonth(firstDayInMonth)); // 10
Console.WriteLine(hijri.GetDayOfMonth(firstDayInMonth)); // 11

Your second block of code was just setting the gregorian date "1/1/1433" so when you were inspecting it you weren't getting a hirji date, you were just getting the date you gave it in the 15th century.

Looking at http://msdn.microsoft.com/en-us/library/system.globalization.hijricalendar.aspx and seeing the methods there should give you a better idea of what you should be doing on the calendar object and what should happen on the DateTime object.

Share:
10,584

Related videos on Youtube

SShebly
Author by

SShebly

currently a Software Engineer

Updated on May 25, 2022

Comments

  • SShebly
    SShebly almost 2 years

    what is the correct behavior for the Calendar objected passed to the constructor of DateTime type?

    I have the components year, month and day as the below example:

    day = 1
    month = 5 
    year = 1433 (which is the current Hijri year)
    

    when creating a datetime object using the below code the result is a valid Greg Date

    HijriCalendar hijri = new HijriCalendar();
    //Get the First Day in the Month
    DateTime firstDayInMonth = new DateTime(1433, month, 1, hijri);
    

    while using the below code generates a valid Hijri date:

    GregorianCalendar greg = new GregorianCalendar();
    //Get the First Day in the Month
    DateTime firstDayInMonth = new DateTime(1433, month, 1, greg);
    

    is that a correct result?

    • Oded
      Oded over 12 years
      Seeing that a specific date would be on both calendars, what's the problem exactly?
    • ChrisBD
      ChrisBD over 12 years
      Are you trying to convert Hijri to Gregorian, so that if you entered Hijri year 1433 you got Gregorian year 2011?
    • SShebly
      SShebly over 12 years
      @ChrisBD : yes I want to get a Gregorian date by passing components considered to be Hijri and vice-versa
  • SShebly
    SShebly over 12 years
    my question is not about the date it self, its about why when passing a Calendar of type Gregorian the result is a Hijri date..
  • Oded
    Oded over 12 years
    @SShebly - A DateTime is a DateTime. It doesn't have a calendar attached to it.