Class
DateFormatter
A formatter that converts between dates and their textual representations.
--在日期及其文本表示形式之间进行转换的格式化程序。将date作为formatter的参数进行格式化输出
Declaration --声明
class DateFormatter : Formatter
Overview --概览
Instances of DateFormatter
create string representations of NSDate
objects, and convert textual representations of dates and times into NSDate
objects. For user-visible representations of dates and times, DateFormatter
provides a variety of localized presets and configuration options. For fixed format representations of dates and times, you can specify a custom format string.
--DateFormatter的实例创建NSDate对象的字符串表示,并将日期和时间的文本表示转换为NSDate对象。对于用户可见的日期和时间表示,DateFormatter提供了各种本地化预置和配置选项。对于日期和时间的固定格式表示,可以指定自定义格式字符串。
When working with date representations in ISO 8601 format, use ISO8601DateFormatter
instead.
--使用ISO8601格式的日期表示时,请使用ISO8601DateFormatter。
To represent an interval between two NSDate
objects, use DateIntervalFormatter
instead.
--要表示两个NSDate对象之间的间隔,请使用DateIntervalFormatter代替
To represent a quantity of time specified by an NSDateComponents
object, use DateComponentsFormatter
instead.
--要表示NSDateComponents对象指定的时间量,请使用DateComponentsFormatter。
Working With User-Visible Representations of Dates and Times
--使用用户可见的日期和时间表示
When displaying a date to a user, you set the dateStyle
and timeStyle
properties of the date formatter according to your particular needs. For example, if you want to show the month, day, and year without showing the time, you would set the dateStyle
property to DateFormatter.Style.long
and the timeStyle
property to DateFormatter.Style.none
. Conversely, if you want to show only the time, you would set the dateStyle
property to DateFormatter.Style.none
and the timeStyle
property to DateFormatter.Style.short
. Based on the values of the dateStyle
and timeStyle