我正在尝试将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 vs 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
答案 0 :(得分:4)
datee = 1434-02-27 23:19:16 +0000
这可能是伊斯兰日历的特色吗?就像伊斯兰教一样,祈祷的时间与日出方程有关,它可以试图在某个点上表达太阳时间。即在沙特阿拉伯,太阳时间是出于宗教原因的唯一有效时间。他们拒绝给同一块土地一次。如果太阳位于最高点,则为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会考虑您的默认时区。
答案 1 :(得分:2)
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与您的本地区域之间存在时区差异。
答案 2 :(得分:1)
// 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]);
答案 3 :(得分:0)
这里所有的答案都是好的方法,但是只是从科学的角度来看。但是,实际上,当开发人员居住在土耳其并且知道这个问题的根源时,苹果公司有多个伊斯兰日历标识符。
- NSCalendarIdentifierIslamic
- NSCalendarIdentifierIslamicCivil
- NSCalendarIdentifierIslamicTabular
- NSCalendarIdentifierIslamicCivilUmmAlQura
我认为他正在寻找的是NSCalendarIdentifierIslamicCivil
。我的应用程序中也有回历日历,NSCalendarIdentifierIslamicCivil
解决了该问题