Date

public extension Date
  • Returns self as milliseconds since 1970

    Declaration

    Swift

    var millisecondsSince1970: Int { get }
  • Initializes a date from milliseconds since 1970

    Declaration

    Swift

    init(millisecondsSince1970: Int)

Extended Date Formats

  • Formats the self per format string, using TimeZone.current

    Declaration

    Swift

    func formatted(fmt: String, locale: Locale? = nil) -> String

    Parameters

    fmt

    a valid DateFormatter format string

    locale

    a valid locale identifier (defaults to current locale)

    Return Value

    date+time string

computed properties, return local date strings

  • Returns the local date string like “May 2019” Deprecated, because in slavonic languages it uses the month name in the genitive case e.g. hr: “veljače 2022”, pl: “lutego 2022”, ru: “февраля 2022” however, in this situation the nominative case would be appropriate

    Declaration

    Swift

    @available(*, deprecated, message: "use LLLL_yyyy instead")
    var MMMM_yyyy: String { get }
  • Returns the local date string like “February 2022” In slavonic languages, uses the month name in the nominative case as appropriate in this situation e.g. hr: “veljača 2022”, pl: “luty 2022”, ru: “февраль 2022”

    Declaration

    Swift

    var LLLL_yyyy: String { get }
  • Returns the local date string like “10 February 2022” In slavonic languages, uses the month name in the genitive case e.g. hr: “10 veljače 2022”, pl: “10 lutego 2022”, ru: “10 февраля 2022” as appropriate in this situation

    Declaration

    Swift

    var dd_MMMM_yyyy: String { get }
  • Returns the local date string like “18.08.2019”

    Declaration

    Swift

    var ddMMyyyy: String { get }
  • Returns the local date string including day, like “Sunday 18.08.2019”

    Declaration

    Swift

    var EEEEddMMyyyy: String { get }
  • Returns the local time string like “20:44:23”

    Declaration

    Swift

    var HHmmss: String { get }
  • Returns the local time string like “20:44”

    Declaration

    Swift

    var HHmm: String { get }
  • Returns the local time string with milliseconds, like “12:00:00.000”

    Declaration

    Swift

    var HHmmssSSS: String { get }
  • Returns a dateTimeString with microsecond resolution, like “2020-10-25 15:42:05.286747”

    Declaration

    Swift

    var ddMMyyyy_HHmmss_𝜇s: String { get }
  • Returns the ISO8601 string representation of self (UTC timezone)

    Declaration

    Swift

    var iso8601UTC: String { get }
  • Returns the ISO8601 string representation of self (local timezone)

    Declaration

    Swift

    var iso8601Local: String { get }
  • Initializes self to the date at the specified secondsInto21stCentury

    Declaration

    Swift

    init(seconds secondsInto21stCentury: TimeInterval)

    Parameters

    secondsInto21stCentury

    seconds since 00:00:00 UTC on 1 January 2001

  • Initializes self to the date specified in the ISO8601 string

    Declaration

    Swift

    init?(iso8601String: String)

    Parameters

    fromISO8601String

    like “2018-04-20T14:20:00-07:00”

  • Initializes self to the date specified in the ISO8601 string

    Declaration

    Swift

    init?(iso8601UTC: String)

    Parameters

    fromISO8601String

    like “2018-04-20T14:20:00-07:00”

  • Initializes self to the date specified in the ISO8601 string

    Declaration

    Swift

    init?(iso8601Local: String)

    Parameters

    fromISO8601String

    like “2018-04-20T14:20:00-07:00”

  • Returns the detailed local date-time string, like “24.07.2019 10:00:00”

    Declaration

    Swift

    var ddMMyyyy_HHmmss: String { get }
  • Returns the detailed local date-time string, like “24.07.2019 10:25”

    Declaration

    Swift

    var ddMMyyyy_HHmm: String { get }
  • Returns the detailed local date-time string, like “Wednesday 24.07.2019 10:00:00”

    Declaration

    Swift

    var EEEE_ddMMyyyy_HHmmss: String { get }
  • Returns the detailed local date-time string, like “Wednesday 24.07.2019 10:00”

    Declaration

    Swift

    var EEEE_ddMMyyyy_HHmm: String { get }
  • Returns the detailed local date-time string, like “Wednesday 24.07.2019 10:00:00 +02:00”

    Declaration

    Swift

    var EEEE_ddMMyyyy_HHmmss_ZZZZZ: String { get }
  • Returns a timestamp (timeIntervalSince1970)

    Declaration

    Swift

    var timeStamp: TimeInterval { get }
  • Returns a timestamp string (timeIntervalSince1970), like “1566153863_69661”

    Declaration

    Swift

    var timeTag: String { get }

modifiers

  • Increments self by component and value

    Declaration

    Swift

    mutating func increment(by component: Calendar.Component, times value: Int = 1)

    Parameters

    component

    a Calendar.Component like .hour, .day, .month, …

    value

    number of compoents (hous, days, months, …)

  • Date incremented by component and value

    Declaration

    Swift

    func incremented(by component: Calendar.Component, times value: Int = 1) -> Date
  • Increments self by 1 month

    Declaration

    Swift

    mutating func nextMonth()
  • Decrements self by 1 month

    Declaration

    Swift

    mutating func prevMonth()
  • Returns a date with the day of month modified

    Preserves the .hour, sets .minute and smaller components to 0

    Declaration

    Swift

    func setting(day: Int) -> Date?

    Parameters

    day

    day of month (1…) to set to

    Return Value

    modified copy of self or nil if invalid date would be generated

  • Modifies self, setting the day of month

    Preserves the .hour, sets .minute and smaller components to 0

    Declaration

    Swift

    mutating func set(day: Int)

    Parameters

    day

    day of month (1…) to set to

  • Returns a date with the hour modified

    Sets .minute and smaller components to 0

    Declaration

    Swift

    func setting(hour: Int) -> Date?

    Parameters

    hour

    hour to set to (0…23)

    Return Value

    modified copy of self or nil if invalid date would be generated

  • Modifies self, setting the hour

    Sets .minute and smaller components to 0

    Declaration

    Swift

    mutating func set(hour: Int)

    Parameters

    hour

    hour to set to (0…23)

  • Returns a date where components smaller than calendarComponent are set to 0

    Example let date = Date().whole(.hour) returns the date at the start of current hour

    Declaration

    Swift

    func whole(_ calendarComponent: Calendar.Component) -> Date?

    Parameters

    calendarComponent

    one of [.year, .month, .day, .hour, .minute, .second]

    Return Value

    modified date

  • Returns a date setting the minute, second all to 0

    Declaration

    Swift

    var wholeHour: Date? { get }

    Return Value

    modified copy of self or nil if invalid date would be generated

  • Returns a date setting the hour, minute, second all to 0

    Declaration

    Swift

    var wholeDay: Date? { get }

    Return Value

    modified copy of self or nil if invalid date would be generated

  • Returns a date setting the day to 1, hour, minute, second all to 0

    Declaration

    Swift

    var wholeMonth: Date? { get }

    Return Value

    modified copy of self or nil if invalid date would be generated

properties

  • Returns month (1..12)

    Declaration

    Swift

    var month: Int { get }
  • Returns month (0..11)

    Declaration

    Swift

    var month_0: Int { get }
  • Returns the start date of the month

    Declaration

    Swift

    var month1st: Date { get }
  • Returns the last date of the month

    Declaration

    Swift

    var monthLast: Date { get }
  • Returns an array of days. ex. [1, 2, …, 31]

    Declaration

    Swift

    var daysInMonth: [Int] { get }
  • Returns year

    Declaration

    Swift

    var year: Int { get }
  • day

    Returns day in month (1…)

    Declaration

    Swift

    var day: Int { get }
  • Returns day in month (0…)

    Declaration

    Swift

    var day_0: Int { get }
  • Returns weekdayOrdinal (range 1…5, 1 for 1st 7 days of the month, 2 for next 7 days, etc)

    Declaration

    Swift

    var weekdayOrdinal: Int { get }
  • Returns weekday (1…7, 1 is Sunday)

    Declaration

    Swift

    var weekday: Int { get }
  • Returns weekday (0…6, 0 is Monday) of the first day of the month

    Declaration

    Swift

    var weekday_0M: Int { get }
  • Returns weekday (1…7, 1 is Sunday) of the first day of the month

    Declaration

    Swift

    var month1stWeekday: Int { get }
  • Returns weekday (0…6, 0 is Monday) of the first day of the month

    Declaration

    Swift

    var month1stWeekday_0M: Int { get }
  • Returns true if self is today (any hour)

    Declaration

    Swift

    var isToday: Bool { get }
  • Returns hour (0..23)

    Declaration

    Swift

    var hour: Int { get }
  • Returns the date interval of component duration which contains self

    Declaration

    Swift

    func dateInterval(of component: Calendar.Component) -> DateInterval?

    Parameters

    component

    calendar component

  • Return the date interval of 2 years around self

    Declaration

    Swift

    var twoYearsAround: DateInterval { get }

    Parameters

    component

    calendar component