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

前端 未结 2 1853
长情又很酷
长情又很酷 2021-01-31 05:53

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\

相关标签:
2条回答
  • 2021-01-31 06:09
    // 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]);
    
    0 讨论(0)
  • 2021-01-31 06:14

    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.

    0 讨论(0)
提交回复
热议问题