带有 4 位数年份的 iphone 短日期
我确信我只是错过了一些东西。 但我已经用谷歌搜索了几天,试图找到如何在显示具有 NSDateFormatterShortStyle 样式的 NSDate 时显示 4 位数的年份。 如果您有解决方案,请告诉我。 这是我现在使用的代码。
[NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehavior10_4];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease ];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
// add label for birth
UILabel *birthday = [[UILabel alloc] initWithFrame:CGRectMake(125, 10, 100, 25)];
birthday.textAlignment = UITextAlignmentRight;
birthday.tag = kLabelTag;
birthday.font = [UIFont boldSystemFontOfSize:14];
birthday.textColor = [UIColor grayColor];
birthday.text = [dateFormatter stringFromDate:p.Birth];
I am sure I am just missing something. But I have googled this for days trying to find how to show a 4 digit year when displaying an NSDate with a style of NSDateFormatterShortStyle. Please let me know if you have a solution. Here is the code I am using now.
[NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehavior10_4];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease ];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
// add label for birth
UILabel *birthday = [[UILabel alloc] initWithFrame:CGRectMake(125, 10, 100, 25)];
birthday.textAlignment = UITextAlignmentRight;
birthday.tag = kLabelTag;
birthday.font = [UIFont boldSystemFontOfSize:14];
birthday.textColor = [UIColor grayColor];
birthday.text = [dateFormatter stringFromDate:p.Birth];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
以防万一有人像我一样在这里绊倒,因为我需要这样一个带有本地化日期格式的两位数年份的东西,这是一个非常老的问题,这是我的解决方案:
也许有人可以在某个时候使用它......
Just in case that someone stumbles in here as I did 'cos I needed such a thing with a 2 digit year in a localized date format and this is a really old question, here is my solution:
Maybe someone could use it sometime...
如果您需要四位数年份,请使用您想要的确切格式设置
dateformat:
。 缺点是您会丢失自动区域设置格式。如果您需要本地化,那么您可以尝试修改简短样式的日期格式。
已更新 Max Macleod 的错误修复
If you need four digit years, set the
dateformat:
using the exact format you'd like. The downside is you lose automatic locale formatting.If you need localization then you could try modifying the date format of the short style.
Updated with Bugfix from Max Macleod
这是一个旧线程,但因为我试图做戴夫想要但不能做的事情,并且因为给出的答案都不正确,所以这里是解决方案(以防有人想要同样的事情):
This is an old thread, but because I was trying to do what Dave wanted and couldn't and because none of the given answers were correct, here is the solution (in case anyone ever want's the same thing):
来源
NSDateFormatterShortStyle 似乎没有给你 4 位数的年份。
Source
NSDateFormatterShortStyle doesn't appear to give you 4 digit years.