Skip to content

Instantly share code, notes, and snippets.

@ohlulu
Created July 18, 2020 02:33
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
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