NSCalendar日期错误 [英] NSCalendar date error

查看:76
本文介绍了NSCalendar日期错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to use NSCalendar with NSIslamicCalendar identifier. But the day result is not good, her's my code:

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 vs 23:19:16)之间有所不同。

As you can see the first date is correct not the 2 other, and also 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).

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

this code is in my viewDidload: function and no other code is running before or after.

此外,如果我想要获取一个月内我得到一天的天数:

Also if i want get the number of days in a month i get one more day:

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.

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

what is the problem and how can i fix it ? thank you.

编辑:

它对于 NSGregorianCalendar 似乎是相同的,一个区别是日计数是正确的。
log使用 NSGregorianCalendar

it seems to be the same for NSGregorianCalendar in one difference is that the day count is correct. log using 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 也许这是伊斯兰日历的特色?就像伊斯兰教一样,祈祷的时间与日出方程有关,它可以试图在某个点上表达太阳时间。即在沙特阿拉伯,太阳时间是出于宗教原因的唯一有效时间。他们拒绝给同一块土地一次。如果太阳位于最高点,则为12:00。因为在相隔几百公里的城市中差别很大,沙特阿拉伯的城市有不同的时间。

datee = 1434-02-27 23:19:16 +0000 maybe this is a specialty of the islamic calendar? as in Islam the times of prayers are related to the sunrise equation it could try to express the sun time on a certain point. i.E. in Saudi Arabia the Sun time is the only valid time for religious reasons. they refuse to give the same land one time. If the sun is at the highest point, it is 12:00. as this differs a lot in cities just few hundred kilometers apart, the cities of Saudi Arabia have different times.

注意,虽然在任何其他伊斯兰国家使用正常的时区,这个实现是有道理的,因为伊斯兰日历最有效的用例可能是宗教事件的计算 - 如果我使用太阳时,它会变得容易得多。

Note, that although in any other islamic country normal timezones are used, this implementation would make sense, as probably the most valid use case for the islamic calendar are calculation of religious events — and it becomes much easier, if I use Sun time.

使用此代码

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

NSDateFormatter takes in account your default timezone.

这篇关于NSCalendar日期错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆