Skip to content

Commit

Permalink
Add date extension
Browse files Browse the repository at this point in the history
  • Loading branch information
aromanov91 committed Oct 5, 2022
1 parent 2d055e4 commit 2a446af
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions Sources/OversizeCore/Extensions/Swift/Date+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,42 @@ public extension Date {
}
}

public extension Date {
func years(from date: Date) -> Int {
Calendar.current.dateComponents([.year], from: date, to: self).year ?? 0
}

/// Returns the amount of months from another date
func months(from date: Date) -> Int {
Calendar.current.dateComponents([.month], from: date, to: self).month ?? 0
}

/// Returns the amount of weeks from another date
func weeks(from date: Date) -> Int {
Calendar.current.dateComponents([.weekOfMonth], from: date, to: self).weekOfMonth ?? 0
}

/// Returns the amount of days from another date
func days(from date: Date) -> Int {
Calendar.current.dateComponents([.day], from: date, to: self).day ?? 0
}

/// Returns the amount of hours from another date
func hours(from date: Date) -> Int {
Calendar.current.dateComponents([.hour], from: date, to: self).hour ?? 0
}

/// Returns the amount of minutes from another date
func minutes(from date: Date) -> Int {
Calendar.current.dateComponents([.minute], from: date, to: self).minute ?? 0
}

/// Returns the amount of seconds from another date
func seconds(from date: Date) -> Int {
Calendar.current.dateComponents([.second], from: date, to: self).second ?? 0
}
}

extension Date: RawRepresentable {
private static let formatter: ISO8601DateFormatter = .init()

Expand Down

0 comments on commit 2a446af

Please sign in to comment.