2

我正在尝试使用NSCalendar标识符NSIslamicCalendar。但是当天结果不好,她是我的代码:

NSCalendar *calandar = [[NSCalendar alloc]initWithCalendarIdentifier:NSIslamicCalendar];    
NSDateComponents *components = [calandar components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:[NSDate date]];
NSInteger theDay = [components day];
NSInteger theMonth = [components month];
NSInteger theYear = [components year];


[components setDay:theDay];
[components setMonth:theMonth];
[components setYear:theYear];
NSDate *thisDate = [calandar dateFromComponents:components];    
NSLog(@"year=%i month=%i day=%i",theYear,theMonth,theDay);
NSLog(@"thisdate = %@", [thisDate description]);


NSString *strDate = [NSString stringWithFormat:@"%i-%i-%i",theYear,theMonth,theDay];
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"yyyy-MM-dd"];
NSDate *datee = [formatter dateFromString:strDate];
NSLog(@"datee = %@", [datee description]);

日志:

year=1434 month=2 day=28              ----> all good (it's islamic calandar)
thisdate = 2013-01-09 23:00:00 +0000  ----> not good (we are in 2013-01-10 not 09).
datee = 1434-02-27 23:19:16 +0000     ----> not good (we are in 1434-02-28 not 27)

正如您所看到的,第一个日期是正确的,而不是另一个日期,而且时间也不正确(当我编译时它大约是 14:20)并且它在最后 2 个最后一个日志之间有所不同(23:00:00 与 23:19: 16)。

此代码在我的 viewDidload: 函数中,之前或之后没有其他代码在运行。

另外,如果我想获得一个月中的天数,我会再获得一天:

NSCalendar *c = [[NSCalendar alloc]initWithCalendarIdentifier:NSIslamicCalendar];
NSRange dayRange = [c rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:date];
NSLog(@"dayRange.length = %i",dayRange.length);

日志:

dayRange.length = 30 -----> but this islamic month have 29 day not 30.

有什么问题,我该如何解决?谢谢你。

编辑:

似乎是相同的NSGregorianCalendar,其中一个区别是天数是正确的。使用日志NSGregorianCalendar

 year=2013 month=1 day=10                -----> all good
 thisdate = 2013-01-09 23:00:00 +0000    -----> not good
 datee = 2013-01-09 23:00:00 +0000       -----> not good

 dayRange.length = 31 ------------------------> all good
4

4 回答 4

4

datee = 1434-02-27 23:19:16 +0000也许这是伊斯兰历法的特色?在伊斯兰教中,祈祷的时间与日出方程式有关,它可以尝试在某个点上表达太阳时间。iE 在沙特阿拉伯,由于宗教原因,太阳时间是唯一有效的时间。他们拒绝给同一块土地一次。如果太阳处于最高点,则为 12:00。由于这在相距仅几百公里的城市中差异很大,因此沙特阿拉伯的城市有不同的时间。

请注意,尽管在任何其他伊斯兰国家都使用正常时区,但这种实现是有意义的,因为伊斯兰日历最有效的用例可能是计算宗教事件——如果我使用太阳时间,它会变得容易得多。


使用此代码

    NSCalendar *islamicCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSIslamicCalendar];

     NSDate *today = [NSDate date];
    [islamicCalendar rangeOfUnit:NSDayCalendarUnit startDate:&today interval:NULL forDate:today];

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setCalendar:islamicCalendar];
    [dateFormatter setTimeStyle:NSDateFormatterFullStyle];
    [dateFormatter setDateStyle:NSDateFormatterFullStyle];

    //english output 
    dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];

    NSString * islamicDateString = [dateFormatter stringFromDate:today];
    NSLog(@"%@", islamicDateString);

结果是

2013-01-11 01:13:46.110 islamicCalendarTest[18346:303] Friday, Safar 29, 1434 12:00:00 AM Central European Standard Time

NSDateFormatter 会考虑您的默认时区。

于 2013-01-10T18:21:16.320 回答
2

time is not correct (when i compile it's about 14:20) and it differs between the last 2 last log (23:00:00 vs 23:19:16).

这是因为 GMT 和您当地区域之间的时区差异。

于 2013-01-10T13:51:11.613 回答
1
// 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]);
于 2013-06-18T06:18:45.220 回答
0

这里的所有答案都是好方法,但只是从科学的角度来看。但事实上,作为一名居住在土耳其并知道这个问题的根源的开发人员,Apple 拥有不止一个伊斯兰日历标识符

- NSCalendarIdentifierIslamic
- NSCalendarIdentifierIslamicCivil
- NSCalendarIdentifierIslamicTabular
- NSCalendarIdentifierIslamicCivilUmmAlQura

我认为他正在寻找的是NSCalendarIdentifierIslamicCivil。我的应用程序里面还有一个回历日历并NSCalendarIdentifierIslamicCivil解决了这个问题

于 2020-05-20T15:52:37.143 回答