views:

631

answers:

3

Hello, I am working on an application in which i save Current Date in 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 Arabic Language?

+3  A: 

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.

Andrew Grant
how do i change arabic locale to a fixed english locale?
Rahul Vyas
Look at NSFormatter setLocale - you can pass in any locale identifier you wish.
Andrew Grant
Great Answer. Thanks...
Biranchi
+1  A: 

Try this

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"EST"];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate:localDate];
zapping
A: 

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];
Rahul Vyas