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

前端 未结 2 1852
长情又很酷
长情又很酷 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]);
    

提交回复
热议问题