8

I'm trying to display the Islamic current day name , month name. I used NSLocale to set the region to ar_SA, however it's just translating the day and month names from English to Arabic.

Output I'm getting in Arabic:

١٢ الأحد ، اكتوبر ، ٢٠١٢ which in English is Sunday, October 28, 2012

Output I want:

Al-Ahad, Dul-Hijja 12, 1433

Snippet:

NSDateFormatter *islamicFormatter = [[NSDateFormatter alloc]init];
[islamicFormatter setDateStyle:NSDateFormatterFullStyle];
[islamicFormatter setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"ar_SA"]];
NSString *islamicDateString = [islamicFormatter stringFromDate:islamicDate];
NSLog(@"%@",islamicDateString);

I hope it's clear for you guys.

3
  • are hebrew date and arabic date same ?
    – Pradip
    Oct 28, 2012 at 15:07
  • Please excuse me. I checked the difference and it is totally different. Please check the answer below and let me know if that helps.
    – Pradip
    Oct 28, 2012 at 15:20
  • they are totally different :)
    – Sobiaholic
    Oct 28, 2012 at 23:47

2 Answers 2

10

I think this would help

NSCalendar * hijri = [[NSCalendar alloc] initWithCalendarIdentifier:NSIslamicCalendar];

NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterLongStyle];
[formatter setTimeStyle:NSDateFormatterNoStyle];
[formatter setCalendar:hijri];

NSLog(@"hijri: %@", [formatter stringFromDate:[NSDate date]]);
[formatter release];
3
  • you can check the detail at stackoverflow.com/questions/4354337/…
    – Pradip
    Oct 28, 2012 at 15:20
  • Actually I checked this answer before I posted my question. However, this code will only display the numeric date. dd/mm/yyyy . I think the key problem is in NSDateFormatter particularly in setDateStyle.
    – Sobiaholic
    Oct 28, 2012 at 16:39
  • I knew it! I missed the setCalendar option. I feel stupid. Been checking the API for 4 hours and this was just right in front of my eyes! thank you!!
    – Sobiaholic
    Oct 28, 2012 at 23:39
3

I know this post is quite old but I think it is necessary to add Swift 3 code as below for other reference.

let hijri = Calendar(identifier: .islamic)

let formatter = DateFormatter()
formatter.calendar = hijri
formatter.dateFormat = "d MMMM yyyy"

let today = Date()
let dateString = formatter.string(from: today)

print(dateString)

// the output: 16 Jamada II 1438

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.