hijri (islamic) calendar problem!

旧巷老猫 提交于 2019-12-03 09:04:07

Assuming this is for a Mac (Cocoa) or iOS (Cocoa Touch) app, since that's where you see Objective C most often, then you can just do something like this:

// Create a Gregorian Calendar
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

// Set up components of a Gregorian date
NSDateComponents *gregorianComponents = [[NSDateComponents alloc] init];

gregorianComponents.day = 4;
gregorianComponents.month = 12;
gregorianComponents.year = 2010;

// Create the date
NSDate *date = [gregorianCalendar dateFromComponents:gregorianComponents];

[gregorianComponents release];
[gregorianCalendar release];


// 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", 
          [hijriComponents day],
          [hijriComponents month],
          [hijriComponents year]);

[hijriCalendar release];

If all you want is the current date, then you can skip setting up the gregorian date altogether and just do this:

// Create an Islamic calendar
NSCalendar *hijriCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSIslamicCalendar];

// And grab the date components for the current date
NSDateComponents *hijriComponents = [hijriCalendar components:(NSDayCalendarUnit | 
                                                               NSMonthCalendarUnit |
                                                               NSYearCalendarUnit)
                                                     fromDate:[NSDate date]];

[hijriCalendar release];
mpenkov

Have a look at this topic: how to convert hijari date into gregorian date in java script?

The question mentions JavaScript but the top answer seems to have links to implementations in a variety of languages.

You should be able to do this in Objective-C (if that really is an option) using NSCalendar.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!