Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a gregorian date string to Islamic date gives correct & incorrect results

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.

like image 655
JAHelia Avatar asked Apr 24 '13 14:04

JAHelia


People also ask

How do you convert Gregorian year to Hijri Year?

Year Gregorian = Year Muslim × 0.97 + 622. If you want to convert a Gregorian date to a Muslim date, you need to subtract 622 from the original year and then multiply by 1.03: Year Muslim = (Year Gregorian– 622) × 1.03.

What is the difference between Hijri dates and Gregorian dates?

Given that the Islamic New Year does not begin January 1 and that a Hijri calendar year is about 11 days shorter than a Gregorian calendar year, there is no direct correspondence between years of the two eras. A given Hijri year will usually fall in two successive Gregorian years.

How do you write the date in the Islamic calendar?

Like most of the world, dates in Arabic format look like this: day/month/year. February 15, 2019, appears as 15/2/2019.


2 Answers

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.

like image 141
zimmryan Avatar answered Oct 05 '22 23:10

zimmryan


// 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]);
like image 37
A R Avatar answered Oct 06 '22 00:10

A R