如何使用NSDateFormatter来查看“今日”string

苹果在今天的消息应用程序中使用它今天11:45 AM昨天11:45 AM

我在苹果开发者网站上看到它

要为date格式化程序指定自定义固定格式,请使用setDateFormat :. 格式string使用Unicode技术标准#35中的格式模式。 标准的版本随着操作系统的发布而变化:

日历字段

字段(别名|(字段*,特殊*))>字段(别名|(显示名称?,相对*,特殊*))>

为日历字段的名称(日历元素,如日,月,年,小时等)提供翻译,并为这些字段提供相对值(例如,相对值为-1的date为“昨天”)。 如果某一特定语言中没有方便的习惯用语或短语来表示相对价值,则应予以省略。

如何在Xcode中使用它?

使用– setDoesRelativeDateFormatting:

 NSDateFormatter *dateFormatter = [NSDateFormatter new]; dateFormatter.dateStyle = NSDateFormatterShortStyle; dateFormatter.timeStyle = NSDateFormatterShortStyle; dateFormatter.doesRelativeDateFormatting = YES; NSLog(@"%@", [dateFormatter stringFromDate:[NSDate date]]); 

这将loggingToday, 11:30 AM在en_US区域设置。

要获取date名称,您可以简单地使用NSDateFormatter:

 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"EEEE"]; NSString *dayName = [dateFormatter stringFromDate:yourDate]; 

所以date格式是: "EEEE"