我正在尝试使用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