简体   繁体   中英

How to get particular date format in Hijri in iOS?

I want to get date in hijri calender in below format

1/جماد الأول/ 1438

but i don;t how can mix these locales.I tried setting the locale but it does not give me 1/1438.I am using below code.

NSCalendar * hijri = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierIslamicCivil];
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
NSLocale *locale  = [[NSLocale alloc]initWithLocaleIdentifier:@"ar_SA"];
[formatter setLocale:locale];
[formatter setCalendar:hijri];
dateString =[formatter stringFromDate:date];

Please tell how can i do this?

There is nothing wrong is your code, you need to add one more attribute in NSDateFormatter .

Add below line in your code:

[formatter setDateFormat:@"dd/yyyy"];

Set your desire date format in above line

Final code will be:

NSCalendar * hijri = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierIslamic];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSLocale *locale  = [[NSLocale alloc]initWithLocaleIdentifier:@"ar_SA"];
[formatter setLocale:locale];
[formatter setCalendar:hijri];
[formatter setDateFormat:@"dd/yyyy"];
NSString *dateString =[formatter stringFromDate:date];
NSLog(@" dateString :%@", dateString);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM