回答
我不认为日可以通过格式化来实现。虽然你可以做这样的:
NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"EEEE, MMMM d"];
NSString *dateString = [dateFormat stringFromDate:today];
NSCalendar *cal = [NSCalendar currentCalendar];
unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponents *dtComp = [cal components:unitFlags fromDate:today];
switch ([dtComp day]) {
case 1:
case 31:
case 21:
dateString = [dateString stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%i",[dtComp day]] withString:[NSString stringWithFormat:@"%ist",[dtComp day]]];
break;
case 2:
case 22:
dateString = [dateString stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%i",[dtComp day]] withString:[NSString stringWithFormat:@"%ind",[dtComp day]]];
break;
case 3:
case 23:
dateString = [dateString stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%i",[dtComp day]] withString:[NSString stringWithFormat:@"%ird",[dtComp day]]];
break;
default:
dateString = [dateString stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%i",[dtComp day]] withString:[NSString stringWithFormat:@"%ith",[dtComp day]]];
break;
}
[dateFormat release];
我不知道谁低估了我的答案,但我很快意识到我的错误。 +1给你指出第一个后缀! – BoltClock 2011-02-18 13:27:17
@Robin Help with NSDateFormatter在这种情况下会帮助你很多NSDateFormatter
,你真的必须努力获得这种格式,只需看看我与你分享的链接。
祝你好运!
感谢的人。对此,我真的非常感激。谢谢 – Robin 2011-02-18 16:12:41
如果有可能以可读的方式我会在Madhups回答评论张贴长注释。
因此,这里是我的评论:
与这些自定义日期格式的坏事是,如果你从美国或使用相同的日期格式其他国家都没有的日期是错误的。
这就是你的解决方案将如何看待我的设备:Freitag,Februar 18日
我的母亲会说th
?那是什么意思?
我会说“另一位从未离开过他的国家的开发者。”
请我想我的约会对象是这样的:Freitag的,18 Februar
,因为这是我已经习惯了。这是一个约会的样子。
所以请添加只使用自定义日期格式化,如果当前区域是DATEFORMAT您正在使用类似的定制的支票。
在iOS4中,Apple引入了一个非常酷的方法。
+ (NSString *)dateFormatFromTemplate:(NSString *)template options:(NSUInteger)opts locale:(NSLocale *)locale
你通过这个方法,你在你的日期格式希望每一个部件,你回来使用的语言环境的格式。
所以,如果你想使用的工作日中,日,月,你会使用这样的:
NSString *localizedDateFormat = [NSDateFormatter dateFormatFromTemplate:@"EEEE MMMM d" options:0 locale:[NSLocale currentLocale]];
,它会返回EEEE, d. MMMM
为德语语言环境。或者美国区域的EEEE, MMMM d
。然后你可以使用它来设置一个自定义的dateFormat。
这就是NSDateFormatter的重点。本地化日期。如果您使用[dateFormatter setDateFormat:@"some hard coded date format"]
向用户显示日期,则在大多数情况下您做错了。
我真的很讨厌那个混美国日期风格和德国的日期风格的应用程序。
- 1. 如何日期格式至周五,8月5日
- 2. SQL Server:SELECT上周五下午12点到这个星期五12 PM
- 3. 更改格式日期从“2014年7月18日星期五”到7/18/14 Rails
- 4. 更改日期格式,从12年12月12日至2012年12月12日iOS6的
- 5. 日期是本周,本月,今年
- 6. “2017-03-29 18:17:12 + 00”的日期格式是什么
- 7. NSDateFormatter提前一年预约12/30/2012和2012年12月31日
- 8. 格式化日期如下午12:30 12年12月12日在JavaScript中
- 9. 列出周一到周五的月份,从周一到周日碳
- 10. 本月的周日数
- 11. DateTime.ParseExact为:星期五12月07日16时36分21秒2012
- 12. NSDateFormatter报告2013年6月2日为零周期
- 13. 获取本周的周一和周五的日期(PHP)
- 14. NSDateFormatter这种格式
- 15. 周五03月30日00:00:00 CET 14 DD/MM/YYYY
- 16. NSDateFormatter总是返回时,我格式化一周或一个月,为什么?
- 17. 转换日期日(2011年5月12日至12日)
- 18. .NET给了我2008年12月29日错误的周数吗?
- 19. 12个月13周平均运行
- 20. 用NSDateFormatter获取“今日”,“明天”,“星期五”风格日期有简单的方法吗?
- 21. 12个月均线,日期
- 22. 12月31日UIDatePicker问题
- 23. 日期12月视图
- 24. PHP MySQL 7日,周,周一至周五的日历分页?
- 25. 以PDF格式显示日期的问题2014年1月12日12:10
- 26. 你如何格式的日期时间,如“2012年12月11日12:00:00”
- 27. 改变从2011-12-08的日期格式,以2011年12月8日
- 28. 不同风格的日期和月份
- 29. Javascript日期格式/解析toString()'2012年12月1日'
- 30. javascript日期格式2010年6月23日上午12:00
看看[这个要点](https://gist.github。COM/JigsChanchiya/6044680#文件suffixdate毫米)。 – cjd 2014-02-15 11:26:58