iphone – 如何使用NSDateFormatter来查看“今天”字符串

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

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

To specify a custom fixed format for a date formatter, you use
setDateFormat:. The format string uses the format patterns from the
Unicode Technical Standard #35. The version of the standard varies
with release of the operating system:

Calendar Fields

fields ( alias | (field*, special*)) > field ( alias | (displayName?,
relative*, special*)) >

Translations may be supplied for names of calendar fields (elements of
a calendar, such as Day, Month, Year, Hour, and so on), and for
relative values for those fields (for example, the day with relative
value -1 is “Yesterday”). Where there is not a convenient, customary
word or phrase in a particular language for a relative value, it
should be omitted.

如何在Xcode中使用它?

使用– setDoesRelativeDateFormatting:

NSDateFormatter *dateFormatter = [NSDateFormatter new];

dateFormatter.dateStyle = NSDateFormatterShortStyle;
dateFormatter.timeStyle = NSDateFormatterShortStyle;

dateFormatter.doesRelativeDateFormatting = YES;

NSLog(@"%@", [dateFormatter stringFromDate:[NSDate date]]);

这个日志今天上午11:30在en_US地区。

翻译自:https://stackoverflow.com/questions/18682206/how-to-use-nsdateformatter-to-see-today-string

转载注明原文:iphone – 如何使用NSDateFormatter来查看“今天”字符串