Changing Numeric Characters for NSDateFormatter

So my scenario is basically this: if the user has Arabic, the date is formatted to this line: 10/19/2012 3:21 م

The numbers are Arabic, and the date is ordered / formatted in a certain way according to the format of the Arabic language / region. The problem is that some users may have the Arabic language standard, but the language is set to English (because he / she may not read Arabic).

Is there a way to change only the numbers to Arabic while keeping everything else in the formatting from Locale? That is, format this date according to the language / region format (storage order), but use English numbers, not Arabic numbers. I do not see any way that Apple provided us with such changes.

Thanks in advance!

+4
source share
2 answers

You must use one of the locale identifiers that have English numeric characters. For instance:

NSDateFormatter *dateFormatter = [NSDateFormatter new]; dateFormatter.dateFormat = @"EEEE dd MMM | hh:mm a"; dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:@"ar_TN"]; NSLog(@"%@", [dateFormatter stringFromDate:[NSDate date]]); 

This will print:

 الخميس 14 جانفي | 02:02 م 
0
source

The following post may answer your question: Replacing strings in Objective-C

 NSString *str = @"This is a string"; str = [str stringByReplacingOccurrencesOfString:@"string" withString:@"duck"]; 

For each formatted date string, you will call it 10 times (0,1,2, ..., 8,9). Perhaps this will help you.

-1
source

Source: https://habr.com/ru/post/1440888/


All Articles