iOS 7中的NSDateFormatter字符串 [英] NSDateFormatter Strings in iOS 7

查看:98
本文介绍了iOS 7中的NSDateFormatter字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在iOS 7中格式化日期,并在星期几获得一些意外的结果。 Apple的文档没有列出iOS 7的模式,但为iOS 6提供的模式是这里。看看星期几

Attempting to format a date in an iOS 7 and getting some unexpected results for day of the week. Apple's documentation does not list patterns for iOS 7, but the patterns provided for iOS 6 are here. Looking at the day of the week section


星期几 - 在短时间内使用一到三个字母,或者在全天使用四个字母,或
五个狭义的名称。

Day of week - Use one through three letters for the short day, or four for the full name, or five for the narrow name.

但是我无法获得4个字母的全名格式化字符串工作。

However I'm unable to get the 4 letter full name formatter string to work.

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"EEE"];
NSLog(@"%@", [formatter stringFromDate:date]);

打印Wed

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"EEEE"];
NSLog(@"%@", [formatter stringFromDate:date]);

还打印Wed,应打印星期三

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"EEEEE"];
NSLog(@"%@", [formatter stringFromDate:date]);

打印W

是否一组新的格式化字符串应该用于iOS 7或者我正在做不正确的事情?

Is there a new set of formatting strings that should be used for iOS 7 or am I doing something incorrectly?

推荐答案

正如其他人注意到的那样,它看起来像一个地区问题。

As others have noticed, it looks like a locale issue.

尝试强制一个已知的语言环境,如下所示

Try to force a known locale like follows

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
// or
NSLocale *locale = [NSLocale currentLocale];
[formatter setLocale:locale];
[formatter setDateFormat:@"EEEE"];
NSLog(@"%@", [formatter stringFromDate:date]);

这篇关于iOS 7中的NSDateFormatter字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆