Skip to content

Instantly share code, notes, and snippets.

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
You can’t perform that action at this time.