This repository has been archived by the owner on Apr 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into feature/mutable-leaf-tag-config
- Loading branch information
Showing
1 changed file
with
22 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,40 @@ | ||
import Foundation | ||
|
||
public extension Date { | ||
public static func startOfMonth() -> Date { | ||
public static func startOfMonth() -> Date? { | ||
return Date().startOfMonth() | ||
} | ||
|
||
public static func endOfMonth() -> Date { | ||
public static func endOfMonth() -> Date? { | ||
return Date().endOfMonth() | ||
} | ||
|
||
public func startOfMonth() -> Date { | ||
public func startOfMonth() -> Date? { | ||
return Calendar.current.date( | ||
from: Calendar.current.dateComponents([.year, .month], | ||
from: Calendar.current.startOfDay(for: self)) | ||
)! | ||
) | ||
} | ||
|
||
public func endOfMonth() -> Date { | ||
public func endOfMonth() -> Date? { | ||
guard let startOfMonth = self.startOfMonth() else { return nil } | ||
return Calendar.current.date( | ||
byAdding: DateComponents(month: 1, day: -1), | ||
to: self.startOfMonth() | ||
)! | ||
to: startOfMonth | ||
) | ||
} | ||
|
||
public func add(days: Int) -> Date? { | ||
return Calendar.current.date( | ||
byAdding: DateComponents(day: days), | ||
to: self | ||
) | ||
} | ||
|
||
public func sub(days: Int) -> Date? { | ||
return Calendar.current.date( | ||
byAdding: DateComponents(day: -days), | ||
to: self | ||
) | ||
} | ||
} |