Set NSDate 0 seconds in swift

This is how to do it in Swift 3.

In this example I remove the seconds in the date components:

let date = picker.date
let calendar = Calendar.current
let components = calendar.dateComponents([.year, .month, .day, .hour, .minute], from: date)
let fullMinuteDate = calendar.date(from: components)!

Working on a playground: Removing the seconds component from a date


From this answer in Swift:

var date = NSDate();
let timeInterval = floor(date .timeIntervalSinceReferenceDate() / 60.0) * 60.0
date = NSDate(timeIntervalSinceReferenceDate: timeInterval)

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)