Skip to content

Instantly share code, notes, and snippets.

@ohlulu
Created July 18, 2020 02:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ohlulu/33cb78e298744d6fc4c8fae23e6fdd65 to your computer and use it in GitHub Desktop.
Save ohlulu/33cb78e298744d6fc4c8fae23e6fdd65 to your computer and use it in GitHub Desktop.
Swift @propertyWrapper for Date
extension Date {
func add(_ component: Calendar.Component, value: Int) -> Date {
return Calendar.current.date(byAdding: component, value: value, to: self)!
}
}
@propertyWrapper
struct BeginEndDate {
enum `Type` {
case begin, end
}
private var date: Date = Date()
private var type: Type = .begin
var wrappedValue: Date {
get {
if type == .begin {
return Calendar.current.startOfDay(for: date)
} else {
var newDate = date.add(.day, value: 1)
newDate = Calendar.current.startOfDay(for: newDate)
newDate = newDate.add(.second, value: -1)
return newDate
}
}
set { date = newValue }
}
init(date: Date, type: Type) {
self.date = date
self.type = type
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment