Date

extension Date
  • Initialize Date values with year, month, and day (as integers).

    Declaration

    Swift

    public init?(year: Int, month: Int, day: Int)
  • Get specified date component from a Date value.

    Declaration

    Swift

    public func getDateComponent(_ componentType: Calendar.Component) -> Int
  • Get an array of specified date component from a Date value.

    Declaration

    Swift

    public func getDateComponents(_ componentsSet: Set<Calendar.Component>) -> DateComponents
  • Get an array of specified date component from a Date value.

    Declaration

    Swift

    public func getDateComponents(_ components: [Calendar.Component]) -> DateComponents

Custom Format

  • Initialize a Date value from a string in the specified format.

    Declaration

    Swift

    public init?(
        string dateTimeString: String,
        format formatString: String,
        locale: Locale = .init(identifier: "en_US_POSIX")
    )

    Parameters

    dateTimeString

    Required. The input string to parse.

    formatString

    Required. The format string in which dateTimeString is supplied.

    locale

    Optional. The Locale to use when parsing dateTimeString. Default is en_US_POSIX.

  • Convert the Date value to a string in the specified format.

    Declaration

    Swift

    public func getDateTimeString(
        withFormat formatString: String,
        in locale: Locale = .init(identifier: "en_US_POSIX")
    ) -> String

    Parameters

    formatString

    Required. The format string in which a string should be returned.

    locale

    Optional. The Locale to use when formatting date string. Default is en_US_POSIX.

    Return Value

    String. The string representation of the date.

YYYY-MM-DD String

  • Initialize a Date value from a year-month-day String, formatted asyyyy-MM-dd (e.g. 1984-01-24).

    Declaration

    Swift

    public init?(ymdString: String)

    Parameters

    ymdString

    Date formatted as yyyy-MM-dd.

  • Convert the Date value to a String formatted as yyyy-MM-dd (e.g. 1984-01-24).

    Declaration

    Swift

    public var ymdString: String { get }

RFC 3339 String

  • Initialize dates from RFC 3339-compliant strings.

    • RFC 3339 strings have the following format: yyyy-MM-dd'T'HH:mm:ss.SSSZ (e.g. 1984-01-24T09:41:55.123-0800).
    • They can have or not have a colon in time zone.

    Declaration

    Swift

    public init?(
        fromRFC3339String rfc3339String: String,
        withColonInTimeZone colonInTimeZone: Bool
    )

    Parameters

    rfc3339String

    An RFC 3339-compliant string

    colonInTimeZone

    Set to true to accept colons in time zone

  • Convert dates to RFC 3339-compliant strings.

    • RFC 3339 strings have the following format: yyyy-MM-dd'T'HH:mm:ss.SSSZ (e.g. 1984-01-24T09:41:55.123-0800).
    • They can have or not have a colon in time zone.

    Declaration

    Swift

    public func getRFC3339String(withColonInTimeZone colonInTimeZone: Bool) -> String

    Parameters

    colonInTimeZone

    Set to true to add colon in time zone

ISO8601 String

Date and Time Strings

  • Convert dates to date and time strings in selected locale and with selected date and time style.

    Declaration

    Swift

    public func getDateTimeString(
        in locale: Locale = Locale.current,
        dateStyle: DateFormatter.Style = .long,
        timeStyle: DateFormatter.Style = .short
    ) -> String

    Parameters

    locale

    see Locale

    dateStyle

    see DateFormatter.Style

    timeStyle

    see DateFormatter.Style

  • Convert dates to date strings with selected date style.

    Declaration

    Swift

    public func getDateString(withStyle style: DateFormatter.Style = .long) -> String
  • Convert dates to time strings with selected time style.

    Declaration

    Swift

    public func getTimeString(withStyle style: DateFormatter.Style = .short) -> String

Age Calculation

  • Calculate the difference between the date and now.

    Declaration

    Swift

    public func getDifference(in unitsSet: Set<Calendar.Component>) -> DateComponents
  • Calculate the difference between the date and now.

    Declaration

    Swift

    public func getDifference(in units: [Calendar.Component]) -> DateComponents
  • Calculate the difference between the date and now (in specified units).

    Declaration

    Swift

    public func getDifference(in unit: Calendar.Component) -> Int?
  • Calculate the difference between the date and now (in years).

    Declaration

    Swift

    public var ageInYears: Int? { get }