Вопрос

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]);

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)

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).

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.

Edit:

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 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.


With this code

    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);

it results in

2013-01-11 01:13:46.110 islamicCalendarTest[18346:303] Friday, Safar 29, 1434 12:00:00 AM Central European Standard Time

NSDateFormatter takes in account your default timezone.

Другие советы

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).

This is because of time zone difference between GMT and your local zone.

// 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]);

All the answers here are good approaches, but just from scientific perspective. But indeed as a developer lives in Turkey and know what this issue stems from, Apple has more than one islamic calendar identifiers

- NSCalendarIdentifierIslamic
- NSCalendarIdentifierIslamicCivil
- NSCalendarIdentifierIslamicTabular
- NSCalendarIdentifierIslamicCivilUmmAlQura

I think what he is looking for is NSCalendarIdentifierIslamicCivil. My app also has a Hijri calendar inside it and NSCalendarIdentifierIslamicCivil resolved the issue

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top