17

I have the following two date strings: (1) 24/04/2013 and (2) 19/03/2013 I'm trying to convert these dates into Islamic (Um Al Qura) dates, I'm using this code block to do so:

    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    df.dateFormat = @"dd/MM/yyyy";
    df.calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate *dateInGrogrian = [df dateFromString:@"24/04/2013"];

    NSDateFormatter *df2 = [[NSDateFormatter alloc] init];
    NSCalendar * cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSIslamicCalendar];
    [df2 setCalendar:cal];
    [df2 setDateFormat:@"dd/MM/yyyy"];
    NSLog(@"Converted date to Islamic = %@",[df2 stringFromDate:dateInGrogrian]); 

if the input string was 24/04/2013 the NSLog displayed:

Converted date to Islamic = 14/06/1434 which is correct (according to the formal Islamic calendar which is used in all Islamic countries). But if the input string was 19/03/2013 the NSLog displayed:

Converted date to Islamic = 08/05/1434 which is incorrect (according to the Islamic calendar the correct date must be 07/05/1434 which's 1 day behind).

-Notes to consider before you suggest an answer:

(1) I have tried to use the calendar identifier NSIslamicCivilCalendar instead of NSIslamicCalendar , but to no avail: one of the converted dates was correct and the other was wrong (1 day behind).

(2) I have tried to use GMT time zone like this: [df2 setTimeZone : [NSTimeZone timeZoneWithName:@"GMT"]]; , this produced a correct converted date for day (2) but incorrect for day (1) (1 day behind).

(3) I have tried combinations of solutions: NSIslamicCivilCalendar with / without GMT time zone, NSIslamicCalendar with / without GMT time Zone, but also to no avail ...

can anybody provide a code block that satisfies both dates ? so I ensure that any provided gregorian date string is correctly converted into Islamic date string.

thank you so much.

JAHelia
  • 6,934
  • 17
  • 74
  • 134
  • 3
    I guess that is due to location specific and daylight savings... I read in an answer by [this DateTime specialist](http://stackoverflow.com/users/106435/vikingosegundo) Read this [Islamic Calender error](http://stackoverflow.com/questions/14259397/nscalendar-date-error/14264680#14264680) this may be help you – Anoop Vaidya Apr 24 '13 at 15:14
  • @AnoopVaidya the link you've provided did not solve the problem, it just gives general info about how Islamic calendar works, I've tried the code sample that's provided in the answer but it didn't make any difference. – JAHelia Apr 27 '13 at 11:53
  • While this may not answer the reason for this discrepancy, this may help in shedding some light: if you convert 10/04/2013, you get 30/05/1434. I don't know anything about the Islamic calendar, but my understanding from Wikipedia/online converters is that this 5th month is only 29 days this year, so this could be a clue to the discrepancy. – Nobosi Apr 28 '13 at 19:14
  • @Nobosi converting 10/04/2013 yields 29/05/1434 not 30/05/1434 according to the formal islamic calendar (provided in a link in the question above) – JAHelia Apr 29 '13 at 05:52
  • 3
    @JAHelia that's my point. The Apple code is changing that to 30/05/1434 which, according to the link, is not a valid day this calendar year. This would seem to suggest a bug in Apple's implementation and so I'd suggest opening a bug report with Apple giving that example (of 10/04/2013) and seeking clarification from them as to why this is the expected behavior given that this day should not seemingly exist. – Nobosi Apr 29 '13 at 15:50
  • I got your point Nobosi, and I filed a detailed bug to Apple and I wish they do some action in the close future. – JAHelia Apr 30 '13 at 06:05
  • the URL you mentioned, http://www.islamicfinder.org/Hcal/hdate_next.php If we enter any date, it is saying, that *There is a small probability of one day error. Please check that once. – Bharath May 02 '13 at 06:10
  • 1
    I'm not an expert but I understand the reason why Apple would not be able to solve such a problem. The islamic calendar is a lunar based calendar. Some months' length (29 or 30) like "Ramadan" are still nowadays decided by observing the moon at the 29th day evening (also for religion reasons). Difficulties in observing the sky may happen in two close countries having different decisions on a month's length. Unless in the future islamic countries decide to follow more scientific ways to unify the calendar, there will always be printed calendars in the same country with different dates. – Moxy May 02 '13 at 08:18
  • People in islamic countries know that problem and so they usually correct with a pen the error in a printed calendar. I can't tell what would be the solution for an iOS app but for sure it would be an understood error. In fact, for administration/serious stuff, Gregorian calendar is used to avoid confusion. – Moxy May 02 '13 at 08:22
  • @Moxy there are two types of Islamic calendars: (1) the Hijri Calendar, which depends on lunar observation (as you said) and may differ from country to another and is undependable for important stuff. (2) the Um Al Qura calendar, which is the formal calendar used in Saudi Arabia and is dependable for all important events (religious and non-religious), this calendar is based on a complex mathematical & astronomical equations to determine the exact length of the month without the need for lunar observation, the link I provided in the question depends on Um Al Qura Calendar. – JAHelia May 04 '13 at 06:23

2 Answers2

19

After printing out all the dates from the last 4 years it seems that Apple uses a somewhat accepted formula for mathematically calculating the Islamic Calendar, since as Moxy pointed out, the actual calendar is not based on scientific days but instead a lunar approximation.

This formula is that all odd numbered months have 30 days and all even numbered months have 29 days with an extra day added to the last month in any year in which the number year mod 30 is one of the following: 2, 5, 7, 10, 13, 16, 18, 21, 24, 26, or 29.

The result is an average month length of 29.53056 days, which is quite close to the lunar month of 29.53059 days.

This formula has been used elsewhere in computers, as it is the only method to calculate the date without using a lookup table for every year in history. As such unless you write the lookup table yourself, this will be as accurate as you can get for all dates going backwards and forwards.

zimmryan
  • 1,099
  • 10
  • 19
  • thanks for the explanation, but it's not the only method used in computers to calculate an islamic date, as I mentioned in the question, there are dates calculated differently from the actual Um Al Qura (islamic) calendar. I'm still stuck at this point. – JAHelia May 04 '13 at 06:27
  • Remember the Islamic Calendar is based on actual moon sightings. This is I think where the discrepancy lies. Mathematically calculated you can get a result but thats where their is some discrepancy. – logixologist Jul 12 '13 at 00:18
8
// Create a Gregorian Calendar
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

// Set up components of a Gregorian date
NSDateComponents *gregorianComponents = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[NSDate date]];

NSLog(@"[In Gregorian calendar ->] Day: %ld, Month: %ld, Year:%ld",
      (long)[gregorianComponents day],
      (long)[gregorianComponents month],
      (long)[gregorianComponents year]);


gregorianComponents.day = [gregorianComponents day];
gregorianComponents.month = [gregorianComponents month];
gregorianComponents.year = [gregorianComponents year];

// Create the date
NSDate *date = [gregorianCalendar dateFromComponents:gregorianComponents];



// Then create an Islamic calendar
NSCalendar *hijriCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSIslamicCivilCalendar];

// And grab those date components for the same date
NSDateComponents *hijriComponents = [hijriCalendar components:(NSDayCalendarUnit |
                                                               NSMonthCalendarUnit |
                                                               NSYearCalendarUnit)
                                                     fromDate:date];


NSLog(@"[In Hijri calendar ->] Day: %ld, Month: %ld, Year:%ld",
      (long)[hijriComponents day],
      (long)[hijriComponents month],
      (long)[hijriComponents year]);
A R
  • 461
  • 4
  • 14
  • +1 Worked Perfectly,, I noticed that the calendar is a day behind, does it use "umm al qura" calendar or other one?? – Aziz Jan 27 '14 at 10:13
  • not working for Europa/istanbul. i tried both NSIslamicCalendar and NSIslamicCivilCalendar. When not succesfull, i added calendarGregor.timezone=[nsTimeZone SytemTimeZone] and calendarHijri.timezone=[nsTimeZone SytemTimeZone]. Still not working – Add080bbA Jan 08 '15 at 12:57