Date
struct Date : ReferenceConvertible, Comparable, Equatable
-
Date from now with updated component(s).
Date(day: -1) // Yesterday at the same hour/minute Date(year: 1) // Next year at the same day/month
- year: The year to add.
- month: The month to add.
- day: The day to add.
- hour: The hour to add.
- minute: The minute to add.
second: The second to add.
Declaration
Swift
public init(year: Int? = nil, month: Int? = nil, day: Int? = nil, hour: Int? = nil, minute: Int? = nil, second: Int? = nil)
Return Value
a Date.
-
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.
-
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.
-
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.
-
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.
-
Get number of years between two date.
Declaration
Swift
public func yearsSince(_ date: Date) -> Int
Parameters
date
date to compate self to.
Return Value
number of years between self and given date.
-
Check if date is within today.
Date().isToday -> true
Declaration
Swift
public var isToday: Bool { get }
-
Generate intervale of date from midnight to midnight net day.
Date().dayInterval(for: 31-01-2019 15:34:13) -> (start: 31-01-2019 00:00:00, end: 31-01-2019 23:59:59)
Declaration
Swift
public func dayInterval(for date: Date) -> (start: Date, end: Date)