I have an NSDate
object and I want to set it to an arbitrary time (say, midnight) so that I can use the timeIntervalSince1970
function to retrieve data consistently without worrying about the time when the object is created.
I've tried using an NSCalendar
and modifying its components by using some Objective-C methods, like this:
let date: NSDate = NSDate()
let cal: NSCalendar = NSCalendar(calendarIdentifier: NSGregorianCalendar)!
let components: NSDateComponents = cal.components(NSCalendarUnit./* a unit of time */CalendarUnit, fromDate: date)
let newDate: NSDate = cal.dateFromComponents(components)
The problem with the above method is that you can only set one unit of time (/* a unit of time */
), so you could only have one of the following be accurate:
- Day
- Month
- Year
- Hours
- Minutes
- Seconds
Is there a way to set hours, minutes, and seconds at the same time and retain the date (day/month/year)?