I\'m trying to use NSCalendar
with NSIslamicCalendar
identifier.
But the day result is not good, her\'s my code:
NSCalendar *calanda
// 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]);
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.
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
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.