如何使用NSDateFormatter来查看“今天”串 [英] how to use NSDateFormatter to see "Today" string
问题描述
苹果在消息应用程序中使用它今天消息
今天上午11:45
昨天上午11:45
apple use it in the messages app for today messages Today 11:45 AM Yesterday 11:45 AM
我看到它apple开发者网站
i see it in the apple developer site
要为日期格式化程序指定自定义固定格式,请使用
setDateFormat:。格式字符串使用
Unicode技术标准#35中的格式模式。随着操作系统的发布,该标准的版本变化
:
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:
日历字段
字段(别名|(字段*,特殊*))>字段(别名|(displayName?,
relative *,special *))>
fields ( alias | (field*, special*)) > field ( alias | (displayName?, relative*, special*)) >
翻译可能是提供日历字段的名称(
a日历的元素,如日,月,年,小时等),以及这些字段的
相对值(例如,相对$的日期) b $ b值-1是昨天)。如果某个特定语言中没有方便的,习惯性的
单词或短语作为相对值,则应省略
。
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中使用它?
how to use it in Xcode?
推荐答案
使用 - setDoesRelativeDateFormatting:
NSDateFormatter *dateFormatter = [NSDateFormatter new];
dateFormatter.dateStyle = NSDateFormatterShortStyle;
dateFormatter.timeStyle = NSDateFormatterShortStyle;
dateFormatter.doesRelativeDateFormatting = YES;
NSLog(@"%@", [dateFormatter stringFromDate:[NSDate date]]);
此日志今天上午11:30
在en_US语言环境中。
this logs Today, 11:30 AM
on en_US locales.
这篇关于如何使用NSDateFormatter来查看“今天”串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!