Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iphone sdk:how to convert arabic date format to english?

I am working on an application in which I save the current Date in the database but when my app runs in Arabic language the current date format is changed into Arabic. I mean the date format should be like this 09/02/2010 but the digits are converted to Arabic digits. So how do I convert them back to English digits even if my app running in the Arabic Language?

like image 726
Rahul Vyas Avatar asked Feb 09 '10 08:02

Rahul Vyas


2 Answers

In general it's best to keep dates in an agnostic type and only format them when they must be displayed. A common format is storing the number of seconds since 1970, or another date you choose.

E.g. the following code will display the current time correctly formatted for the users local.

NSDate* now = [NSDate dateWithTimeIntervalSinceNow:0];
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[NSLocale currentLocale]];
NSString* localDateString = [formatter stringFromDate];
[formatter release];

However when you do have a date in a locale-specific format, you can again use the formatter class to convert it. E.g.

NSString* localDate = @"09/02/2010";  // assume this is your string

NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[NSLocale currentLocale]];
NSDate* date = [formatter dateFromString: localDate];
[formatter release];

For working with any type of locale-specific data (dates, currency, measurements) then the formatter classes are your friend.

like image 81
Andrew Grant Avatar answered Sep 20 '22 01:09

Andrew Grant


i found it here i did it like this

NSDate *CurrentDate = [NSDate date];
NSDateFormatter *Formatter=[[NSDateFormatter alloc] init];
NSLocale *indianLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[Formatter setLocale:indianLocale];
[Formatter setDateFormat:@"dd/MM/yyyy"];
NSString *FormattedDate=[Formatter stringFromDate:CurrentDate];
[indianLocale release];
[Formatter release];
like image 34
Rahul Vyas Avatar answered Sep 19 '22 01:09

Rahul Vyas