Date

public extension Date
  • Returns a Date with right now’s time and date.

    Seealso

    Date.init()

    Declaration

    Swift

    static func now() -> Date
  • Returns the hours from a date.

    Seealso

    Calendar().component()

    Declaration

    Swift

    func hours(using calendar: Calendar = .current) -> Int

    Parameters

    calendar

    the calendar to get the date components from. Default: current user calendar.

    Return Value

    An Int value representing the hours, between 0 - 24.

  • Returns the minutes from a date.

    Seealso

    Calendar().component()

    Declaration

    Swift

    func minutes(using calendar: Calendar = .current) -> Int

    Parameters

    calendar

    the calendar to get the date components from. Default: current user calendar.

    Return Value

    An Int value representing the minutes, between 0 - 60.

  • Returns the seconds from a date.

    Seealso

    Calendar().component()

    Declaration

    Swift

    func seconds(using calendar: Calendar = .current) -> Int

    Parameters

    calendar

    the calendar to get the date components from. Default: current user calendar.

    Return Value

    An Int value representing the seconds, between 0 - 60.

  • Returns the day from a date.

    Seealso

    Calendar().component()

    Declaration

    Swift

    func day(using calendar: Calendar = .current) -> Int

    Parameters

    calendar

    the calendar to get the date components from. Default: current user calendar.

    Return Value

    An Int value representing the day of month.

  • Returns the month from a date.

    Seealso

    Calendar().component()

    Declaration

    Swift

    func month(using calendar: Calendar = .current) -> Int

    Parameters

    calendar

    the calendar to get the date components from. Default: current user calendar.

    Return Value

    An Int value representing the month.

  • Returns the year from a date.

    Seealso

    Calendar().component()

    Declaration

    Swift

    func year(using calendar: Calendar = .current) -> Int

    Parameters

    calendar

    the calendar to get the date components from. Default: current user calendar.

    Return Value

    An Int value representing the year.

  • Returns the date components specified by units from a date.

    Seealso

    Calendar().dateComponents()

    Note

    You could pass an array to units, such as [.Hour, .Second] to retrieve both components at the same time.

    Declaration

    Swift

    func components(_ units: Set<Calendar.Component>, using calendar: Calendar = .current) -> DateComponents

    Parameters

    units

    The units to retrieve. ex: .Second

    calendar

    the calendar to get the date components from. Default: current user calendar.

    Return Value

    The requested date components.

  • Creates a Date from a string.

    Declaration

    Swift

    init?(dateString: String, dateFormatter: DateFormatter = DefaultDateFormatter)

    Parameters

    dateString

    The date string representation.

    dateFormatter

    The formatter used for initializing the date. By default, formatter which uses format “yyyy-MM-dd” and US locale.

  • Creates a Date from a string and a string format.

    Declaration

    Swift

    init?(dateString: String, formatString: String)

    Parameters

    dateString

    The date string representation.

    formatString

    The format string used for initializing the date.

  • Creates a Date from a day, month and year.

    Seealso

    init(dateString: String)

    Declaration

    Swift

    init?(day: Int, month: Int, year: Int)
  • Returns the current day of the week (ex: Monday).

    Declaration

    Swift

    func getWeekDay(withFormat dateFormatter: DateFormatter = DefaultWeekDateFormatter) -> String

    Parameters

    dateFormatter

    The dateFormatter to use.

  • Returns a new date that is set to a given number of days relative to the date.

    Seealso

    dateByAddingTimeInterval()

    Declaration

    Swift

    func adding(days: Int) -> Date
  • Returns a new date that is set to a given number of hours relative to the date.

    Seealso

    dateByAddingTimeInterval()

    Declaration

    Swift

    func adding(hours: Int) -> Date
  • Returns a the string representation of self with the format specified in the dateFormatter. By default, uses a formatter with format “yyyy-MM-dd” and US locale.

    Declaration

    Swift

    func toString(accordingTo dateFormatter: DateFormatter = DefaultDateFormatter) -> String
  • Returns a the string representation of self with the format specified.

    Declaration

    Swift

    func toString(with format: String) -> String
  • Returns a the string representation of self with the localized format created from the format specified.

    Seealso

    Dateformatter.setLocalizedDateFormatFromTemplate

    Declaration

    Swift

    func toString(withLocalized formatToLocalize: String) -> String