Update date documentation

This commit is contained in:
Daniel Saidi 2021-09-08 11:22:00 +02:00
parent 9521f80e38
commit a453a281e9
4 changed files with 34 additions and 0 deletions

View File

@ -10,30 +10,51 @@ import Foundation
public extension Date {
/**
The number of years between this date and another one.
*/
func years(from date: Date, calendar: Calendar = .current) -> Int {
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 {
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 {
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 {
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 {
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 {
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 {
calendar.dateComponents([.second], from: date, to: self).second ?? 0
}

View File

@ -10,6 +10,10 @@ import Foundation
public extension Date {
/**
Create a date value using the provided components. Year,
month and day are required, while the others are not.
*/
init?(
year: Int,
month: Int,

View File

@ -23,6 +23,11 @@ public extension JSONDecoder {
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
let container = try decoder.singleValueContainer()
let string = try container.decode(String.self)

View File

@ -10,6 +10,10 @@ import Foundation
public extension DateFormatter {
/**
Create a custom date formatter, that uses a custom date
format, calendar, locale and time zone.
*/
convenience init(
dateFormat: String,
calendar: Calendar = Calendar(identifier: .iso8601),