开发者

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

I am working on an ap开发者_StackOverflow中文版plication 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?


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.


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


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


Swift

dateFormatter.locale = NSLocale(localeIdentifier: "en_US") as Locale!
dateFormatter.dateFormat = "dd/MM/yyyy"
let resultsDate = dateFormatter.string(from: date)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜