ios - NS日历日期错误

标签 ios objective-c nsdate nsdateformatter nscalendar

我正在尝试将 NSCalendarNSIslamicCalendar 标识符一起使用。 但是那天的结果并不好,她是我的代码:

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),并且它在最后两个日志之间有所不同(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

最佳答案

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

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


用这段代码

    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 会考虑您的默认时区。

关于ios - NS日历日期错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14259397/

相关文章:

swift - NSDate() 或 Date() 显示错误的时间

android - 导入按钮是否从解析仪表板数据浏览器中删除了?

ios - 将新版本推送到 TestFlight 时,iOS 应用程序中的 Appicon 没有改变

objective-c - Obj-C,在未设置为 'self' 的结果时返回 '[(super or self) init...]'?

objective-c - 为什么不能将pushViewController与UIButton一起使用?

iphone - 如何检查它是从哪个 View Controller 导航的....在obj c中

iphone - run loop/drawRect 中神秘的 "progressive slowing"问题

ios - 访问 CKRecordZone 数组中的字段?

swift - 在 Swift 3 中获取当前日期?

objective-c - IOS 7 引入的 JavaScriptCore.framework Objective-C API 给出了一个错误的 NSDate 转换(BUG?)