Update date documentation
This commit is contained in:
parent
9521f80e38
commit
a453a281e9
|
@ -10,30 +10,51 @@ import Foundation
|
||||||
|
|
||||||
public extension Date {
|
public extension Date {
|
||||||
|
|
||||||
|
/**
|
||||||
|
The number of years between this date and another one.
|
||||||
|
*/
|
||||||
func years(from date: Date, calendar: Calendar = .current) -> Int {
|
func years(from date: Date, calendar: Calendar = .current) -> Int {
|
||||||
calendar.dateComponents([.year], from: date, to: self).year ?? 0
|
calendar.dateComponents([.year], from: date, to: self).year ?? 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
The number of months between this date and another one.
|
||||||
|
*/
|
||||||
func months(from date: Date, calendar: Calendar = .current) -> Int {
|
func months(from date: Date, calendar: Calendar = .current) -> Int {
|
||||||
calendar.dateComponents([.month], from: date, to: self).month ?? 0
|
calendar.dateComponents([.month], from: date, to: self).month ?? 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
The number of weeks between this date and another one.
|
||||||
|
*/
|
||||||
func weeks(from date: Date, calendar: Calendar = .current) -> Int {
|
func weeks(from date: Date, calendar: Calendar = .current) -> Int {
|
||||||
calendar.dateComponents([.weekOfYear], from: date, to: self).weekOfYear ?? 0
|
calendar.dateComponents([.weekOfYear], from: date, to: self).weekOfYear ?? 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
The number of days between this date and another one.
|
||||||
|
*/
|
||||||
func days(from date: Date, calendar: Calendar = .current) -> Int {
|
func days(from date: Date, calendar: Calendar = .current) -> Int {
|
||||||
calendar.dateComponents([.day], from: date, to: self).day ?? 0
|
calendar.dateComponents([.day], from: date, to: self).day ?? 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
The number of hours between this date and another one.
|
||||||
|
*/
|
||||||
func hours(from date: Date, calendar: Calendar = .current) -> Int {
|
func hours(from date: Date, calendar: Calendar = .current) -> Int {
|
||||||
calendar.dateComponents([.hour], from: date, to: self).hour ?? 0
|
calendar.dateComponents([.hour], from: date, to: self).hour ?? 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
The number of minutes between this date and another one.
|
||||||
|
*/
|
||||||
func minutes(from date: Date, calendar: Calendar = .current) -> Int {
|
func minutes(from date: Date, calendar: Calendar = .current) -> Int {
|
||||||
calendar.dateComponents([.minute], from: date, to: self).minute ?? 0
|
calendar.dateComponents([.minute], from: date, to: self).minute ?? 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
The number of seconds between this date and another one.
|
||||||
|
*/
|
||||||
func seconds(from date: Date, calendar: Calendar = .current) -> Int {
|
func seconds(from date: Date, calendar: Calendar = .current) -> Int {
|
||||||
calendar.dateComponents([.second], from: date, to: self).second ?? 0
|
calendar.dateComponents([.second], from: date, to: self).second ?? 0
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,10 @@ import Foundation
|
||||||
|
|
||||||
public extension Date {
|
public extension Date {
|
||||||
|
|
||||||
|
/**
|
||||||
|
Create a date value using the provided components. Year,
|
||||||
|
month and day are required, while the others are not.
|
||||||
|
*/
|
||||||
init?(
|
init?(
|
||||||
year: Int,
|
year: Int,
|
||||||
month: Int,
|
month: Int,
|
||||||
|
|
|
@ -23,6 +23,11 @@ public extension JSONDecoder {
|
||||||
|
|
||||||
private extension JSONDecoder.DateDecodingStrategy {
|
private extension JSONDecoder.DateDecodingStrategy {
|
||||||
|
|
||||||
|
/**
|
||||||
|
This strategy can be used to parse ISO8601 dates. It is
|
||||||
|
more robust than the standard strategy, and will try to
|
||||||
|
parse both milliseconds and seconds.
|
||||||
|
*/
|
||||||
static let robustISO8601 = custom { decoder throws -> Date in
|
static let robustISO8601 = custom { decoder throws -> Date in
|
||||||
let container = try decoder.singleValueContainer()
|
let container = try decoder.singleValueContainer()
|
||||||
let string = try container.decode(String.self)
|
let string = try container.decode(String.self)
|
||||||
|
|
|
@ -10,6 +10,10 @@ import Foundation
|
||||||
|
|
||||||
public extension DateFormatter {
|
public extension DateFormatter {
|
||||||
|
|
||||||
|
/**
|
||||||
|
Create a custom date formatter, that uses a custom date
|
||||||
|
format, calendar, locale and time zone.
|
||||||
|
*/
|
||||||
convenience init(
|
convenience init(
|
||||||
dateFormat: String,
|
dateFormat: String,
|
||||||
calendar: Calendar = Calendar(identifier: .iso8601),
|
calendar: Calendar = Calendar(identifier: .iso8601),
|
||||||
|
|
Loading…
Reference in New Issue