Convert DateTime from English to Spanish in iPhone?

℡╲_俬逩灬. 提交于 2019-12-11 06:32:42

问题


Does anybody know how to convert a DateTime from English to Spanish?

E.g convert:

December 07, 10:42AM

into

Diciembre 07, 10:42AM

Please help me. How to convert this?

Thanks in advance.

I tried this:

 NSLocale *frLocale = [[[NSLocale alloc] initWithLocaleIdentifier:@"es"] autorelease];
 NSLog(@"%@", [frLocale displayNameForKey:NSLocaleIdentifier value:@"December 07, 10:42AM"]);
 // Value comes Null

回答1:


Try like below it will help

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
    [dateFormatter setDateFormat:@"MMMM dd',' hh:mma"];
    NSDate *dateTmp  = [dateFormatter dateFromString:@"December 07, 10:42AM"]; // getting date from string with english locale
    [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"es"]];
    [dateFormatter setAMSymbol:@"AM"]; // default AM symbol for spanish is a.m.
    [dateFormatter setPMSymbol:@"PM"]; // default PM symbol for spanish is p.m.
    NSString *strDate = [dateFormatter stringFromDate:dateTmp]; // getting string from date with spanish locale
    NSLog(@"%@",strDate);


来源:https://stackoverflow.com/questions/13757335/convert-datetime-from-english-to-spanish-in-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!