Date

public extension Date
  • SwifterSwift: Day name format.

    • threeLetters: 3 letter day abbreviation of day name.
    • oneLetter: 1 letter day abbreviation of day name.
    • full: Full day name.
    See more

    Declaration

    Swift

    enum DayNameStyle
  • SwifterSwift: Month name format.

    • threeLetters: 3 letter month abbreviation of month name.
    • oneLetter: 1 letter month abbreviation of month name.
    • full: Full month name.
    See more

    Declaration

    Swift

    enum MonthNameStyle

Properties

  • SwifterSwift: User’s current calendar.

    Declaration

    Swift

    var calendar: Calendar { get }
  • era

    SwifterSwift: Era.

    Date().era -> 1
    

    Declaration

    Swift

    var era: Int { get }
  • SwifterSwift: Quarter.

    Date().quarter -> 3 // date in third quarter of the year.
    

    Declaration

    Swift

    var quarter: Int { get }
  • SwifterSwift: Week of year.

    Date().weekOfYear -> 2 // second week in the year.
    

    Declaration

    Swift

    var weekOfYear: Int { get }
  • SwifterSwift: Week of month.

    Date().weekOfMonth -> 3 // date is in third week of the month.
    

    Declaration

    Swift

    var weekOfMonth: Int { get }
  • SwifterSwift: Year.

    Date().year -> 2017
    
    var someDate = Date()
    someDate.year = 2000 // sets someDate's year to 2000
    

    Declaration

    Swift

    var year: Int { get set }
  • SwifterSwift: Month.

    Date().month -> 1
    
    var someDate = Date()
    someDate.month = 10 // sets someDate's month to 10.
    

    Declaration

    Swift

    var month: Int { get set }
  • day

    SwifterSwift: Day.

    Date().day -> 12
    
    var someDate = Date()
    someDate.day = 1 // sets someDate's day of month to 1.
    

    Declaration

    Swift

    var day: Int { get set }
  • SwifterSwift: Weekday.

    Date().weekday -> 5 // fifth day in the current week.
    

    Declaration

    Swift

    var weekday: Int { get }
  • SwifterSwift: Hour.

    Date().hour -> 17 // 5 pm
    
    var someDate = Date()
    someDate.hour = 13 // sets someDate's hour to 1 pm.
    

    Declaration

    Swift

    var hour: Int { get set }
  • SwifterSwift: Minutes.

    Date().minute -> 39
    
    var someDate = Date()
    someDate.minute = 10 // sets someDate's minutes to 10.
    

    Declaration

    Swift

    var minute: Int { get set }
  • SwifterSwift: Seconds.

    Date().second -> 55
    
    var someDate = Date()
    someDate.second = 15 // sets someDate's seconds to 15.
    

    Declaration

    Swift

    var second: Int { get set }
  • SwifterSwift: Nanoseconds.

    Date().nanosecond -> 981379985
    
    var someDate = Date()
    someDate.nanosecond = 981379985 // sets someDate's seconds to 981379985.
    

    Declaration

    Swift

    var nanosecond: Int { get set }
  • SwifterSwift: Milliseconds.

    Date().millisecond -> 68
    
    var someDate = Date()
    someDate.millisecond = 68 // sets someDate's nanosecond to 68000000.
    

    Declaration

    Swift

    var millisecond: Int { get set }
  • SwifterSwift: Check if date is in future.

    Date(timeInterval: 100, since: Date()).isInFuture -> true
    

    Declaration

    Swift

    var isInFuture: Bool { get }
  • SwifterSwift: Check if date is in past.

    Date(timeInterval: -100, since: Date()).isInPast -> true
    

    Declaration

    Swift

    var isInPast: Bool { get }
  • SwifterSwift: Check if date is within today.

    Date().isInToday -> true
    

    Declaration

    Swift

    var isInToday: Bool { get }
  • SwifterSwift: Check if date is within yesterday.

    Date().isInYesterday -> false
    

    Declaration

    Swift

    var isInYesterday: Bool { get }
  • SwifterSwift: Check if date is within tomorrow.

    Date().isInTomorrow -> false
    

    Declaration

    Swift

    var isInTomorrow: Bool { get }
  • SwifterSwift: Check if date is within a weekend period.

    Declaration

    Swift

    var isInWeekend: Bool { get }
  • SwifterSwift: Check if date is within a weekday period.

    Declaration

    Swift

    var isWorkday: Bool { get }
  • SwifterSwift: Check if date is within the current week.

    Declaration

    Swift

    var isInCurrentWeek: Bool { get }
  • SwifterSwift: Check if date is within the current month.

    Declaration

    Swift

    var isInCurrentMonth: Bool { get }
  • SwifterSwift: Check if date is within the current year.

    Declaration

    Swift

    var isInCurrentYear: Bool { get }
  • SwifterSwift: ISO8601 string of format (yyyy-MM-dd’T'HH:mm:ss.SSS) from date.

    Date().iso8601String -> "2017-01-12T14:51:29.574Z"
    

    Declaration

    Swift

    var iso8601String: String { get }
  • SwifterSwift: Nearest five minutes to date.

    var date = Date() // "5:54 PM"
    date.minute = 32 // "5:32 PM"
    date.nearestFiveMinutes // "5:30 PM"
    
    date.minute = 44 // "5:44 PM"
    date.nearestFiveMinutes // "5:45 PM"
    

    Declaration

    Swift

    var nearestFiveMinutes: Date { get }
  • SwifterSwift: Nearest ten minutes to date.

    var date = Date() // "5:57 PM"
    date.minute = 34 // "5:34 PM"
    date.nearestTenMinutes // "5:30 PM"
    
    date.minute = 48 // "5:48 PM"
    date.nearestTenMinutes // "5:50 PM"
    

    Declaration

    Swift

    var nearestTenMinutes: Date { get }
  • SwifterSwift: Nearest quarter hour to date.

    var date = Date() // "5:57 PM"
    date.minute = 34 // "5:34 PM"
    date.nearestQuarterHour // "5:30 PM"
    
    date.minute = 40 // "5:40 PM"
    date.nearestQuarterHour // "5:45 PM"
    

    Declaration

    Swift

    var nearestQuarterHour: Date { get }
  • SwifterSwift: Nearest half hour to date.

    var date = Date() // "6:07 PM"
    date.minute = 41 // "6:41 PM"
    date.nearestHalfHour // "6:30 PM"
    
    date.minute = 51 // "6:51 PM"
    date.nearestHalfHour // "7:00 PM"
    

    Declaration

    Swift

    var nearestHalfHour: Date { get }
  • SwifterSwift: Nearest hour to date.

    var date = Date() // "6:17 PM"
    date.nearestHour // "6:00 PM"
    
    date.minute = 36 // "6:36 PM"
    date.nearestHour // "7:00 PM"
    

    Declaration

    Swift

    var nearestHour: Date { get }
  • SwifterSwift: Yesterday date.

    let date = Date() // "Oct 3, 2018, 10:57:11"
    let yesterday = date.yesterday // "Oct 2, 2018, 10:57:11"
    

    Declaration

    Swift

    var yesterday: Date { get }
  • SwifterSwift: Tomorrow’s date.

    let date = Date() // "Oct 3, 2018, 10:57:11"
    let tomorrow = date.tomorrow // "Oct 4, 2018, 10:57:11"
    

    Declaration

    Swift

    var tomorrow: Date { get }
  • SwifterSwift: UNIX timestamp from date.

    Date().unixTimestamp -> 1484233862.826291
    

    Declaration

    Swift

    var unixTimestamp: Double { get }

Methods

  • SwifterSwift: Date by adding multiples of calendar component.

    let date = Date() // "Jan 12, 2017, 7:07 PM"
    let date2 = date.adding(.minute, value: -10) // "Jan 12, 2017, 6:57 PM"
    let date3 = date.adding(.day, value: 4) // "Jan 16, 2017, 7:07 PM"
    let date4 = date.adding(.month, value: 2) // "Mar 12, 2017, 7:07 PM"
    let date5 = date.adding(.year, value: 13) // "Jan 12, 2030, 7:07 PM"
    

    Declaration

    Swift

    func adding(_ component: Calendar.Component, value: Int) -> Date

    Parameters

    component

    component type.

    value

    multiples of components to add.

    Return Value

    original date + multiples of component added.

  • SwifterSwift: Add calendar component to date.

    var date = Date() // "Jan 12, 2017, 7:07 PM"
    date.add(.minute, value: -10) // "Jan 12, 2017, 6:57 PM"
    date.add(.day, value: 4) // "Jan 16, 2017, 7:07 PM"
    date.add(.month, value: 2) // "Mar 12, 2017, 7:07 PM"
    date.add(.year, value: 13) // "Jan 12, 2030, 7:07 PM"
    

    Declaration

    Swift

    mutating mutating func add(_ component: Calendar.Component, value: Int)

    Parameters

    component

    component type.

    value

    multiples of compnenet to add.

  • SwifterSwift: Date by changing value of calendar component.

    let date = Date() // "Jan 12, 2017, 7:07 PM"
    let date2 = date.changing(.minute, value: 10) // "Jan 12, 2017, 6:10 PM"
    let date3 = date.changing(.day, value: 4) // "Jan 4, 2017, 7:07 PM"
    let date4 = date.changing(.month, value: 2) // "Feb 12, 2017, 7:07 PM"
    let date5 = date.changing(.year, value: 2000) // "Jan 12, 2000, 7:07 PM"
    

    Declaration

    Swift

    func changing(_ component: Calendar.Component, value: Int) -> Date?

    Parameters

    component

    component type.

    value

    new value of compnenet to change.

    Return Value

    original date after changing given component to given value.

  • SwifterSwift: Data at the beginning of calendar component.

    let date = Date() // "Jan 12, 2017, 7:14 PM"
    let date2 = date.beginning(of: .hour) // "Jan 12, 2017, 7:00 PM"
    let date3 = date.beginning(of: .month) // "Jan 1, 2017, 12:00 AM"
    let date4 = date.beginning(of: .year) // "Jan 1, 2017, 12:00 AM"
    

    Declaration

    Swift

    func beginning(of component: Calendar.Component) -> Date?

    Parameters

    component

    calendar component to get date at the beginning of.

    Return Value

    date at the beginning of calendar component (if applicable).

  • SwifterSwift: Date at the end of calendar component.

    let date = Date() // "Jan 12, 2017, 7:27 PM"
    let date2 = date.end(of: .day) // "Jan 12, 2017, 11:59 PM"
    let date3 = date.end(of: .month) // "Jan 31, 2017, 11:59 PM"
    let date4 = date.end(of: .year) // "Dec 31, 2017, 11:59 PM"
    

    Declaration

    Swift

    func end(of component: Calendar.Component) -> Date?

    Parameters

    component

    calendar component to get date at the end of.

    Return Value

    date at the end of calendar component (if applicable).

  • SwifterSwift: Check if date is in current given calendar component.

    Date().isInCurrent(.day) -> true
    Date().isInCurrent(.year) -> true
    

    Declaration

    Swift

    func isInCurrent(_ component: Calendar.Component) -> Bool

    Parameters

    component

    calendar component to check.

    Return Value

    true if date is in current given calendar component.

  • SwifterSwift: Date string from date.

    Date().string(withFormat: "dd/MM/yyyy") -> "1/12/17"
    Date().string(withFormat: "HH:mm") -> "23:50"
    Date().string(withFormat: "dd/MM/yyyy HH:mm") -> "1/12/17 23:50"
    

    Declaration

    Swift

    func string(withFormat format: String = "dd/MM/yyyy HH:mm") -> String

    Parameters

    format

    Date format (default is “dd/MM/yyyy”).

    Return Value

    date string.

  • SwifterSwift: Date string from date.

    Date().dateString(ofStyle: .short) -> "1/12/17"
    Date().dateString(ofStyle: .medium) -> "Jan 12, 2017"
    Date().dateString(ofStyle: .long) -> "January 12, 2017"
    Date().dateString(ofStyle: .full) -> "Thursday, January 12, 2017"
    

    Declaration

    Swift

    func dateString(ofStyle style: DateFormatter.Style = .medium) -> String

    Parameters

    style

    DateFormatter style (default is .medium).

    Return Value

    date string.

  • SwifterSwift: Date and time string from date.

    Date().dateTimeString(ofStyle: .short) -> "1/12/17, 7:32 PM"
    Date().dateTimeString(ofStyle: .medium) -> "Jan 12, 2017, 7:32:00 PM"
    Date().dateTimeString(ofStyle: .long) -> "January 12, 2017 at 7:32:00 PM GMT+3"
    Date().dateTimeString(ofStyle: .full) -> "Thursday, January 12, 2017 at 7:32:00 PM GMT+03:00"
    

    Declaration

    Swift

    func dateTimeString(ofStyle style: DateFormatter.Style = .medium) -> String

    Parameters

    style

    DateFormatter style (default is .medium).

    Return Value

    date and time string.

  • SwifterSwift: Time string from date

    Date().timeString(ofStyle: .short) -> "7:37 PM"
    Date().timeString(ofStyle: .medium) -> "7:37:02 PM"
    Date().timeString(ofStyle: .long) -> "7:37:02 PM GMT+3"
    Date().timeString(ofStyle: .full) -> "7:37:02 PM GMT+03:00"
    

    Declaration

    Swift

    func timeString(ofStyle style: DateFormatter.Style = .medium) -> String

    Parameters

    style

    DateFormatter style (default is .medium).

    Return Value

    time string.

  • SwifterSwift: Day name from date.

    Date().dayName(ofStyle: .oneLetter) -> "T"
    Date().dayName(ofStyle: .threeLetters) -> "Thu"
    Date().dayName(ofStyle: .full) -> "Thursday"
    

    Declaration

    Swift

    func dayName(ofStyle style: DayNameStyle = .full) -> String

    Parameters

    Style

    style of day name (default is DayNameStyle.full).

    Return Value

    day name string (example: W, Wed, Wednesday).

  • SwifterSwift: Month name from date.

    Date().monthName(ofStyle: .oneLetter) -> "J"
    Date().monthName(ofStyle: .threeLetters) -> "Jan"
    Date().monthName(ofStyle: .full) -> "January"
    

    Declaration

    Swift

    func monthName(ofStyle style: MonthNameStyle = .full) -> String

    Parameters

    Style

    style of month name (default is MonthNameStyle.full).

    Return Value

    month name string (example: D, Dec, December).

  • SwifterSwift: get number of seconds between two date

    Declaration

    Swift

    func secondsSince(_ date: Date) -> Double

    Parameters

    date

    date to compate self to.

    Return Value

    number of seconds between self and given date.

  • SwifterSwift: get number of minutes between two date

    Declaration

    Swift

    func minutesSince(_ date: Date) -> Double

    Parameters

    date

    date to compate self to.

    Return Value

    number of minutes between self and given date.

  • SwifterSwift: get number of hours between two date

    Declaration

    Swift

    func hoursSince(_ date: Date) -> Double

    Parameters

    date

    date to compate self to.

    Return Value

    number of hours between self and given date.

  • SwifterSwift: get number of days between two date

    Declaration

    Swift

    func daysSince(_ date: Date) -> Double

    Parameters

    date

    date to compate self to.

    Return Value

    number of days between self and given date.

  • SwifterSwift: check if a date is between two other dates

    Declaration

    Swift

    func isBetween(_ startDate: Date, _ endDate: Date, includeBounds: Bool = false) -> Bool

    Parameters

    startDate

    start date to compare self to.

    endDate

    endDate date to compare self to.

    includeBounds

    true if the start and end date should be included (default is false)

    Return Value

    true if the date is between the two given dates.

  • SwifterSwift: check if a date is a number of date components of another date

    Declaration

    Swift

    func isWithin(_ value: UInt, _ component: Calendar.Component, of date: Date) -> Bool

    Parameters

    value

    number of times component is used in creating range

    component

    Calendar.Component to use.

    date

    Date to compare self to.

    Return Value

    true if the date is within a number of components of another date

  • SwifterSwift: Returns a random date within the specified range.

    Declaration

    Swift

    static func random(in range: Range<Date>) -> Date

    Parameters

    range

    The range in which to create a random date. range must not be empty.

    Return Value

    A random date within the bounds of range.

  • SwifterSwift: Returns a random date within the specified range.

    Declaration

    Swift

    static func random(in range: ClosedRange<Date>) -> Date

    Parameters

    range

    The range in which to create a random date.

    Return Value

    A random date within the bounds of range.

  • SwifterSwift: Returns a random date within the specified range, using the given generator as a source for randomness.

    Declaration

    Swift

    static func random<T>(in range: Range<Date>, using generator: inout T) -> Date where T : RandomNumberGenerator

    Parameters

    range

    The range in which to create a random date. range must not be empty.

    generator

    The random number generator to use when creating the new random date.

    Return Value

    A random date within the bounds of range.

  • SwifterSwift: Returns a random date within the specified range, using the given generator as a source for randomness.

    Declaration

    Swift

    static func random<T>(in range: ClosedRange<Date>, using generator: inout T) -> Date where T : RandomNumberGenerator

    Parameters

    range

    The range in which to create a random date.

    generator

    The random number generator to use when creating the new random date.

    Return Value

    A random date within the bounds of range.

Initializers

  • SwifterSwift: Create a new date form calendar components.

    let date = Date(year: 2010, month: 1, day: 12) // "Jan 12, 2010, 7:45 PM"
    

    Declaration

    Swift

    init?(
        calendar: Calendar? = Calendar.current,
        timeZone: TimeZone? = NSTimeZone.default,
        era: Int? = Date().era,
        year: Int? = Date().year,
        month: Int? = Date().month,
        day: Int? = Date().day,
        hour: Int? = Date().hour,
        minute: Int? = Date().minute,
        second: Int? = Date().second,
        nanosecond: Int? = Date().nanosecond)

    Parameters

    calendar

    Calendar (default is current).

    timeZone

    TimeZone (default is current).

    era

    Era (default is current era).

    year

    Year (default is current year).

    month

    Month (default is current month).

    day

    Day (default is today).

    hour

    Hour (default is current hour).

    minute

    Minute (default is current minute).

    second

    Second (default is current second).

    nanosecond

    Nanosecond (default is current nanosecond).

  • SwifterSwift: Create date object from ISO8601 string.

    let date = Date(iso8601String: "2017-01-12T16:48:00.959Z") // "Jan 12, 2017, 7:48 PM"
    

    Declaration

    Swift

    init?(iso8601String: String)

    Parameters

    iso8601String

    ISO8601 string of format (yyyy-MM-dd’T'HH:mm:ss.SSSZ).

  • SwifterSwift: Create new date object from UNIX timestamp.

    let date = Date(unixTimestamp: 1484239783.922743) // "Jan 12, 2017, 7:49 PM"
    

    Declaration

    Swift

    init(unixTimestamp: Double)

    Parameters

    unixTimestamp

    UNIX timestamp.

  • SwifterSwift: Create date object from Int literal

    let date = Date(integerLiteral: 2017_12_25) // "2017-12-25 00:00:00 +0000"
    

    Declaration

    Swift

    init?(integerLiteral value: Int)

    Parameters

    value

    Int value, e.g. 20171225, or 2017_12_25 etc.