Rutger Huijsmans Rutger Huijsmans - 2 months ago 8
Swift Question

Setting an NSDate to always use midnight

Right now I set an NSDate object like so:

func userSelectedDate(selectedDate : NSDate) {
let formatter = NSDateFormatter()
formatter.dateFormat = "dd-MM-yyyy"
let dateString = formatter.stringFromDate(selectedDate)
}


I always want to use midnight though. How do I force the usage of a certain time?

Answer
let calendar = NSCalendar.currentCalendar()
let dateAtMidnight = calendar.startOfDayForDate(NSDate())