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.

Sobiaholic
  • 2,927
  • 9
  • 37
  • 54

2 Answers2

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];
Pradip
  • 1,507
  • 11
  • 28
  • you can check the detail at http://stackoverflow.com/questions/4354337/hijri-islamic-calendar-problem – Pradip Oct 28 '12 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 '12 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 '12 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
xmhafiz
  • 3,482
  • 1
  • 18
  • 26