Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 动态日期格式取决于语言和区域设置_Ios_Localization_Nsdateformatter - Fatal编程技术网

Ios 动态日期格式取决于语言和区域设置

Ios 动态日期格式取决于语言和区域设置,ios,localization,nsdateformatter,Ios,Localization,Nsdateformatter,Plz帮助我使用日期格式化程序:我需要根据choosen语言更改日期格式,并选择格式时间24小时/12小时,因此动态更新它是一个主要问题。现在我做得像 [form setDateFormat:[NSString stringWithFormat:@"yyyy-MM-dd HH:mm"]]; NSDate *normalDate = [form dateFromString:time]; if (normalDate == nil) {

Plz帮助我使用日期格式化程序:我需要根据choosen语言更改日期格式,并选择格式时间24小时/12小时,因此动态更新它是一个主要问题。现在我做得像

        [form setDateFormat:[NSString stringWithFormat:@"yyyy-MM-dd HH:mm"]];
        NSDate *normalDate = [form dateFromString:time];

        if (normalDate == nil) {
            [form setDateFormat:[NSString stringWithFormat:@"yyyy-MM-dd h:mm a"]];
            normalDate = [form dateFromString:time];
        }

        [form setDateFormat:[NSString stringWithFormat:@"MMMM dd, yyyy %@",timeF]];
        normalStr = [form stringFromDate:normalDate];

但我认为这是一种非常糟糕的方法,请帮助我制作更好的系统,这也会起作用)

我从未尝试过,但您可以尝试使用Localizable.strings文件。 因此,当你本地化你的应用程序时,在本地化.strings(英语)中你可以

"date_format" = "yyyy-MM-dd HH:mm";
而可能是在本地化。字符串(其他语言)中

在DateFormatter中,您可以

[form setDateFormat:NSLocalizedString(@"date_format", nil)]

不确定你真正想做什么)

考虑使用NSDateFormatter dateFormatFromTemplate:options:locale:: e、 g.具有当前区域设置值([NSLocale currentLocale]) 或者使用[dateFormatter setLocale:…],指定所选语言

NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];

和[dateFormatter setTimeStyle:…]要将其设置为NSDateFormatterShortStyle、NSDateFormatterMiumStyle或NSDateFormatterLongStyle,自iOS 8以来,
dateFormatter
可以通过调用


我考虑过这一点,但麻烦的是,用户可以设置例如英语和12/24小时格式,因此每种语言都有2个位置)
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
NSDateFormatter *formatter = [NSDateFormatter new];
formatter.formattingContext = NSFormattingContextDynamic;
[formatter setLocalizedDateFormatFromTemplate:@"yyyy-MM-dd HH:mm"];