I'm trying to get NSDate
from UIDatePicker
, but it constantly returns me a date time with trailing 20 seconds. How can I manually set NSDate
's second to zero in swift?
extension Date {
var zeroSeconds: Date? {
let calendar = Calendar.current
let dateComponents = calendar.dateComponents([.year, .month, .day, .hour, .minute], from: self)
return calendar.date(from: dateComponents)
}
}
Usage:
let date1 = Date().zeroSeconds
let date2 = Date()
print(date2.zeroSeconds)