我使用以下代码从日期
获取NSString
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = kCFDateFormatterLongStyle;
df.timeStyle = kCFDateFormatterNoStyle;
df.doesRelativeDateFormatting = YES;
[df setLocale:[NSLocale currentLocale]];
[df setTimeZone:[NSTimeZone systemTimeZone]];
NSString *dateString=[df stringFromDate:myDate];
但它显示的类似于'今天'昨天'甚至在更改Locale之后。我想显示其本地化语言吗?
答案 0 :(得分:0)
我使用了你的代码并创建了这个:
NSDate *yesterday;
NSTimeInterval interval;
NSCalendar *cal = [NSCalendar currentCalendar];
[cal rangeOfUnit:NSDayCalendarUnit
startDate:&yesterday
interval:&interval
forDate:[NSDate date]];
yesterday = [yesterday dateByAddingTimeInterval:-interval];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = kCFDateFormatterLongStyle;
df.timeStyle = kCFDateFormatterNoStyle;
df.doesRelativeDateFormatting = YES;
[@[[NSLocale localeWithLocaleIdentifier:@"de_DE"], [NSLocale localeWithLocaleIdentifier:@"es_ES"],[NSLocale localeWithLocaleIdentifier:@"en_US"]] enumerateObjectsUsingBlock:^(NSLocale *locale, NSUInteger idx, BOOL *stop) {
[df setLocale:locale];
[df setTimeZone:[NSTimeZone systemTimeZone]];
NSString *dateString=[df stringFromDate:yesterday];
NSLog(@"%@", dateString);
}];
输出正确
Gestern
ayer
Yesterday
你的问题必须放在别的地方。
在命令行程序中测试
确保在您更改设备/模拟器上的语言后,在再次打开它之前完全杀死该应用程序。
答案 1 :(得分:-2)
您好我为LocalTime编写此函数它将日期输入为String并转换为本地时间字符串。愿它帮助你
+(NSString*)LocalTimeWitheDateString:(NSString*)dateString{
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
NSTimeZone *timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
[formatter setTimeZone:timeZone];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date =[formatter dateFromString:dateString];
NSTimeZone *localTimeZone = [NSTimeZone localTimeZone];
[formatter setTimeZone:localTimeZone];
NSString *LnewTimeZoneDateString = [formatter stringFromDate:date];
return LnewTimeZoneDateString;
}