Date

struct Date : ReferenceConvertible, Comparable, Equatable
  • 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

    public 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

    public enum MonthNameStyle
  • SwifterSwift: User’s current calendar.

    Declaration

    Swift

    public var calendar: Calendar { get }
  • era

    SwifterSwift: Era.

    Date().era -> 1
    

    Declaration

    Swift

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

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

    Declaration

    Swift

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

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

    Declaration

    Swift

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

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

    Declaration

    Swift

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

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

    Declaration

    Swift

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

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

    Declaration

    Swift

    public 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

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

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

    Declaration

    Swift

    public 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

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

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

    Declaration

    Swift

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

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

    Declaration

    Swift

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

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

    Declaration

    Swift

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

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

    Declaration

    Swift

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

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

    Declaration

    Swift

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

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

    Declaration

    Swift

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

    Date().isInToday -> true
    

    Declaration

    Swift

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

    Date().isInYesterday -> false
    

    Declaration

    Swift

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

    Date().isInTomorrow -> false
    

    Declaration

    Swift

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

    Declaration

    Swift

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

    Declaration

    Swift

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

    Declaration

    Swift

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

    Declaration

    Swift

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

    Declaration

    Swift

    public 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

    public 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

    public 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

    public 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

    public 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

    public 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

    public var nearestHour: Date { get }
  • SwifterSwift: Time zone used currently by system.

    Date().timeZone -> Europe/Istanbul (current)
    

    Declaration

    Swift

    public var timeZone: TimeZone { get }
  • SwifterSwift: UNIX timestamp from date.

    Date().unixTimestamp -> 1484233862.826291
    

    Declaration

    Swift

    public var unixTimestamp: Double { get }
  • 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

    public 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

    public 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

    public 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

    public 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

    public 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

    public 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

    public 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

    public 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

    public 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

    public 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

    public 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

    public 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

    public 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

    public 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

    public 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

    public 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

    public 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

    public 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: Random date between two dates.

    Date.random()
    Date.random(from: Date())
    Date.random(upTo: Date())
    Date.random(from: Date(), upTo: Date())
    

    Declaration

    Swift

    public static func random(from fromDate: Date = Date.distantPast, upTo toDate: Date = Date.distantFuture) -> Date

    Parameters

    fromDate

    minimum date (default is Date.distantPast)

    toDate

    maximum date (default is Date.distantFuture)

    Return Value

    random date between two dates.

  • 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

    public init?(
    	calendar: Calendar? = Calendar.current,
    	timeZone: TimeZone? = TimeZone.current,
    	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

    public 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

    public 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

    public init?(integerLiteral value: Int)

    Parameters

    value

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

  • SwifterSwift: Check if date is within the current week.

    Declaration

    Swift

    @available(*, deprecated: 4.1.0, renamed: "isInCurrentWeek", message: "Use isInCurrentWeek instead")
    public var isInThisWeek: Bool { get }
  • SwifterSwift: Check if date is within the current month.

    Declaration

    Swift

    @available(*, deprecated: 4.1.0, renamed: "isInCurrentMonth", message: "Use isInCurrentMonth instead")
    public var isInThisMonth: Bool { get }
  • SwifterSwift: Check if date is within the current year.

    Declaration

    Swift

    @available(*, deprecated: 4.1.0, renamed: "isInCurrentYear", message: "Use isInCurrentYear instead")
    public var isInThisYear: Bool { get }