我正在尝试使用NSCalendar
with 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]);
logs:
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),并且最后两个日志之间存在差异(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);
log:
dayRange.length = 30 -----> but this islamic month have 29 day not 30.
有什么问题以及如何解决它?谢谢。
Edit:
似乎是一样的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