4位数年份的iphone短日期
我肯定我错过了什么。但我已经在谷歌上搜索了好几天,试图找到在显示NSDateFormatterShortStyle样式的NSDate时如何显示4位数的年份。如果你有解决办法,请告诉我。这是我现在使用的代码4位数年份的iphone短日期,iphone,nsdate,nsdateformatter,Iphone,Nsdate,Nsdateformatter,我肯定我错过了什么。但我已经在谷歌上搜索了好几天,试图找到在显示NSDateFormatterShortStyle样式的NSDate时如何显示4位数的年份。如果你有解决办法,请告诉我。这是我现在使用的代码 [NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehavior10_4]; NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] aut
[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];
kCFDateFormatterShortStyle
指定短样式,通常仅为数字,例如“11/23/37”或“3:30pm”
在Mac OS X v10.3及更高版本中提供
在CFDateFormatter.h中声明
NSDateFormatterShortStyle似乎没有为您提供4位数的年份。如果您需要4位数的年份,请使用您想要的确切格式设置
日期格式:
。缺点是会丢失自动区域设置格式
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"MM/dd/yyyy"];
...
birthday.text = [dateFormatter stringFromDate:p.Birth];
如果需要本地化,则可以尝试修改短样式的日期格式
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease ];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
if ([[dateFormatter dateFormat] rangeOfString:@"yyyy"].location == NSNotFound) {
NSString *fourDigitYearFormat = [[dateFormatter dateFormat] stringByReplacingOccurrencesOfString:@"yy" withString:@"yyyy"];
[dateFormatter setDateFormat:fourDigitYearFormat];
}
更新了Max Macleod的错误修复程序,以防有人像我一样在这里绊倒,因为我需要一个本地化日期格式的2位数年份,这是一个非常老的问题,下面是我的解决方案:
NSLocale* curLocale = [NSLocale currentLocale];
NSString* dateFmt = [NSDateFormatter dateFormatFromTemplate: @"ddMMyyyy"
options: 0
locale: curLocale];
NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:dateFmt];
NSString* retText = [formatter stringFromDate:refDate];
return retText;
也许有人会在某个时候使用它…这是一个旧的线程,但因为我试图做Dave想做的事,但做不到,而且给出的答案都不正确,所以这里有一个解决方案(万一有人想做同样的事情):
这是我的观点。没有提供4位数年份的月/日/年格式。如果我输入格式,我将失去本地化。这是我的挫败感。事实上,这取决于地区:丹麦是一个提供4位数年短格式的国家。这是一个很好的测试用例,因为它在times中不使用“:”。要本地化,请添加行dateFormatter.locale=[NSLocale AutoUpdateingCurrentLocale];实际上,如果特定于区域设置的格式已经是yyyy,您还需要进行检查,例如,如果([formatString rangeOfString:@“yyyy”].location==NSNotFound){NSString*fourDigitYearFormat=[[dateFormatter dateFormat]StringByReplacingOfString:@“YYY”和字符串:@“YYYYY”];[dateFormatter setDateFormat:fourDigitYearFormat];}太棒了!我一直在努力解决各种其他解决方案,这一点非常关键。谢谢,伙计。继续做好工作。
NSString *FormatString = [NSDateFormatter dateFormatFromTemplate:@"MM/dd/yyyy HH:mm" options:0 locale:[NSLocale currentLocale]];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:FormatString];
//Parse date here with the formater (like [formatter stringFromDate:date])
[formatter release];