NSCalendardate错误
我试图使用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)
正如你所看到的第一个date是正确的,而不是另外两个,也是时间是不正确的(当我编译大约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
日志logging
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
datee = 1434-02-27 23:19:16 +0000
也许这是伊斯兰日历的特长? 就像在伊斯兰教中,祷告的时间与日出方程有关,它可以试图在某一点上expression日照时间。 伊斯兰教在沙特阿拉伯太阳时间是宗教原因的唯一有效时间。 他们拒绝一次给同一片土地。 如果太阳在最高点,现在是十二点。 因为在相隔数百公里的城市里,这种差异很大,沙特阿拉伯的城市有不同的时代。
请注意,虽然在其他伊斯兰国家使用正常的时区,但这种实现方式是有意义的,因为伊斯兰日历最有效的用例可能是计算宗教事件 – 如果我使用太阳时间,它变得更加容易。
用这个代码
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考虑您的默认时区。
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和您的本地区域之间的时区差异。
// 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]);