Structure
DateComponents
iOS 8.0+
iPadOS 8.0+
Mac Catalyst 8.0+
macOS 10.9+
tvOS 9.0+
watchOS 2.0+
A date or time specified in terms of units (such as year, month, day, hour, minute) to be evaluated in a calendar system and time zone.
struct DateComponents
Overview
DateComponents encapsulates the components of a date in an extendable, structured manner.
It is used to specify a date by providing the temporal components that make up a date and time in a particular calendar: hour, minutes, seconds, day, month, year, and so on. It can also be used to specify a duration of time, for example, 5 hours and 16 minutes. A DateComponents is not required to define all the component fields.
When a new instance of DateComponents is created, the date components are set to nil.
Topics
Structures
struct HTTPFormatStyleConvertsDateComponentsinto RFC 9110-compatible “HTTP date”String, and parses in the reverse direction. This parser does not do validation on the individual values of the components. An optional date can be created from the result usingCalendar(identifier: .gregorian).date(from: ...). When formatting, missing or invalid fields are filled with default values:Sun,01,Jan,2000,00:00:00,GMT. Note that missing fields may result in an invalid date or time. Other values in theDateComponentsare ignored.struct ISO8601FormatStyleOptions for generating and parsing string representations of dates following the ISO 8601 standard.
Operators
Initializers
init(_:strategy:)Creates a newDateComponentsby parsing the given string representation.init(calendar: Calendar?, timeZone: TimeZone?, era: Int?, year: Int?, month: Int?, day: Int?, hour: Int?, minute: Int?, second: Int?, nanosecond: Int?, weekday: Int?, weekdayOrdinal: Int?, quarter: Int?, weekOfMonth: Int?, weekOfYear: Int?, yearForWeekOfYear: Int?)Initializes a date components value, optionally specifying values for its fields.
Instance Properties
var calendar: Calendar?The calendar used to interpret the other values in this structure.var date: Date?The date calculated from the current components using the stored calendar.var day: Int?A day or count of days.var dayOfYear: Int?A day of the year. For example, in the Gregorian calendar, can go from 1 to 365 or 1 to 366 in leap years.var era: Int?An era or count of eras.var hour: Int?An hour or count of hours.var isLeapMonth: Bool?Set to true if these components represent a leap month.var isRepeatedDay: Bool?Set to true if these components represent a repeated day.var isValidDate: BoolIndicates whether the current combination of properties represents a date which exists in the current calendar.var minute: Int?A minute or count of minutes.var month: Int?A month or count of months.var nanosecond: Int?A nanosecond or count of nanoseconds.var quarter: Int?A quarter or count of quarters.var second: Int?A second or count of seconds.var timeZone: TimeZone?A time zone.var weekOfMonth: Int?A week of the month or a count of weeks of the month.var weekOfYear: Int?A week of the year or count of the weeks of the year.var weekday: Int?A weekday or count of weekdays.var weekdayOrdinal: Int?A weekday ordinal or count of weekday ordinals. Weekday ordinal units represent the position of the weekday within the next larger calendar unit, such as the month. For example, 2 is the weekday ordinal unit for the second Friday of the month.var year: Int?A year or count of years.var yearForWeekOfYear: Int?The year corresponding to a week-counting week.
Instance Methods
func formatted<F>(F) -> F.FormatOutputConvertsselfto its textual representation.func hash(into: inout Hasher)func isValidDate(in: Calendar) -> BoolIndicates whether the current combination of properties represents a date which exists in the specified calendar.func setValue(Int?, for: Calendar.Component)Set the value of one of the properties, using an enumeration value instead of a property name.func value(for: Calendar.Component) -> Int?Returns the value of one of the properties, using an enumeration value instead of a property name.
Default Implementations
Relationships
Conforms To
Foundation.ReferenceConvertibleSwift.CopyableSwift.CustomDebugStringConvertibleSwift.CustomReflectableSwift.CustomStringConvertibleSwift.DecodableSwift.EncodableSwift.EquatableSwift.EscapableSwift.HashableSwift.SendableSwift.SendableMetatype