iphoneobjective-ciphone-sdk-3.0nsdate

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?


Solution

  • 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.