Date

struct Date : ReferenceConvertible, Comparable, Equatable

This extension add some useful functions to Date.

  • Set and get current year.

    Declaration

    Swift

    public var year: Int { get set }
  • Set and get current month.

    Declaration

    Swift

    public var month: Int { get set }
  • day

    Set and get current day.

    Declaration

    Swift

    public var day: Int { get set }
  • Set and get current hour.

    Declaration

    Swift

    public var hour: Int { get set }
  • Set and get current minute.

    Declaration

    Swift

    public var minute: Int { get set }
  • Set and get current second.

    Declaration

    Swift

    public var second: Int { get set }
  • Get current nanosecond.

    Declaration

    Swift

    public var nanosecond: Int { get }
  • Get the weekday number from

    • 1 - Sunday.
    • 2 - Monday.
    • 3 - Tuerday.
    • 4 - Wednesday.
    • 5 - Thursday.
    • 6 - Friday.
    • 7 - Saturday.

    Declaration

    Swift

    public var weekday: Int { get }
  • Editable date components.

    • year: Year component.
    • month: Month component.
    • day: Day component.
    • hour: Hour component.
    • minute: Minute component.
    • second: Second component.
    See more

    Declaration

    Swift

    public enum EditableDateComponents : Int
  • Update current Date components.

    Declaration

    Swift

    public mutating func update(components: [EditableDateComponents : Int])

    Parameters

    components

    Dictionary of components and values to be updated.

  • Creates a Date object from year, month and day as Int.

    Declaration

    Swift

    public init?(year: Int, month: Int, day: Int, hour: Int = 0, minute: Int = 0, second: Int = 0)

    Parameters

    year

    Year.

    month

    Month.

    day

    Day.

    hour

    Hour.

    minute

    Minute.

    second

    Second.

  • Creates a Date object from a date String in a given format.

    Declaration

    Swift

    public init?(parse dateString: String, format: String = "yyyy-MM-dd", locale: String = "en_US_POSIX")

    Parameters

    dateString

    Date String.

    format

    Date String format. Default is yyyy-MM-dd. Example: 2014-05-20.

    locale

    Locale, default is en_US_POSIX. You can use Locale.current.identifier.

  • Create a Date with other two Date objects. Taken from the first date: day, month and year. Taken from the second date: hours and minutes.

    Declaration

    Swift

    public init?(date: Date, time: Date, dateSeparator: String = "-", timeSeparator: String = ":")

    Parameters

    date

    The first date for date.

    time

    The second date for time.

    dateSeparator

    Date separator, default is -.

    timeSeparator

    Time separator, default is :.

  • Create an ISO 8601 date from a String.

    Declaration

    Swift

    public init?(iso8601: String)

    Parameters

    date

    ISO 8601 String.

  • Creates an ISO 8601 String form

    Declaration

    Swift

    public func iso8601() -> String

    Return Value

    Returns an ISO 8601 String form

  • Get the months number between self and another date.

    Declaration

    Swift

    public func monthsBetween(_ toDate: Date) -> Int

    Parameters

    toDate

    The another date.

    Return Value

    Returns the months between the two dates.

  • Get the days number between self and another date.

    Declaration

    Swift

    public func daysBetween(_ anotherDate: Date) -> Int

    Parameters

    anotherDate

    The another date.

    Return Value

    Returns the days between the two dates.

  • Returns if self is today.

    Declaration

    Swift

    public func isToday() -> Bool

    Return Value

    Returns if self is today.

  • Compare self with another date.

    Declaration

    Swift

    public func isSame(_ anotherDate: Date) -> Bool

    Parameters

    anotherDate

    The another date to compare as Date.

    Return Value

    Returns true if is same day, otherwise false.

  • Add days to

    Declaration

    Swift

    public func addingDays(_ days: Int) -> Date?

    Parameters

    days

    The number of days to add.

    Return Value

    Returns self by adding the gived days number.

  • Add days to

    Declaration

    Swift

    public mutating func addDays(_ days: Int)

    Parameters

    days

    The number of days to add.

  • Get the year string from

    Declaration

    Swift

    public func yearString() -> String

    Return Value

    Returns the year string from

  • Get the String date from

    Declaration

    Swift

    public func dateString(format: String = "yyyy-MM-dd", locale: String = "en_US_POSIX") -> String

    Parameters

    format

    Date format, default is yyyy-MM-dd.

    locale

    Locale, default is en_US_POSIX.

    Return Value

    Returns the String data from

  • Returns date with the year, month and day only.

    Declaration

    Swift

    public func shortDate() -> Date

    Return Value

    Date after removing all components but not year, month and day.

  • Check if the given date is less than

    Declaration

    Swift

    public func isGreaterThan(_ date: Date) -> Bool

    Parameters

    date

    Date to compare.

    Return Value

    Returns a true if self is greater than another one, otherwise false.

  • Check if the given date is greater than

    Declaration

    Swift

    public func isLessThan(_ date: Date) -> Bool

    Parameters

    date

    Date to compare.

    Return Value

    Returns a true if self is less than another one, otherwise false.

  • Just an alias for isSame(_ anotherDate: Date).

    Declaration

    Swift

    public func isEqual(_ date: Date) -> Bool

    Parameters

    date

    Date to compare.

    Return Value

    Returns a true if self is equal to another one, otherwise false.

  • Create a Date with the yesterday date.

    Declaration

    Swift

    public func yesterday() -> Date

    Return Value

    Returns a Date with the yesterday date.

  • Get weekday as a localized string from current weekday number.

    Declaration

    Swift

    public func localizedWeekday() -> String

    Return Value

    Return weekday as a localized string.

  • Get month as a localized string from current month.

    Declaration

    Swift

    public func localizedMonth() -> String

    Return Value

    Returns the given month as a localized string.

  • Get the given Date structure as a formatted string.

    Declaration

    Swift

    public func description(dateSeparator: String = "/", usFormat: Bool = false, nanosecond: Bool = false) -> String

    Parameters

    info

    The Date to be formatted.

    dateSeparator

    The string to be used as date separator. (Currently does not work on Linux).

    usFormat

    Set if the timestamp is in US format or not.

    nanosecond

    Set if the timestamp has to have the nanosecond.

    Return Value

    Returns a String in the following format (dateSeparator = /, usFormat to false and nanosecond to false). D/M/Y H:M:S. Example: 15/10/2013 10:38:43.