Skip to content
This repository has been archived by the owner on Apr 20, 2024. It is now read-only.

Commit

Permalink
Merge branch 'master' into feature/mutable-leaf-tag-config
Browse files Browse the repository at this point in the history
  • Loading branch information
steffendsommer authored Jun 21, 2018
2 parents 43d8233 + 5193215 commit bee0730
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions Sources/Sugar/Helpers/Date.swift
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
)
}
}

0 comments on commit bee0730

Please sign in to comment.