How can I set an NSDate object to midnight? How can I set an NSDate object to midnight? ios ios

How can I set an NSDate object to midnight?


Your statement

The problem with the above method is that you can only set one unit of time ...

is not correct. NSCalendarUnit conforms to the RawOptionSetType protocol which inherits from BitwiseOperationsType. This means that the options can be bitwisecombined with & and |.

In Swift 2 (Xcode 7) this was changed again to bean OptionSetType which offers a set-like interface, seefor example Error combining NSCalendarUnit with OR (pipe) in Swift 2.0.

Therefore the following compiles and works in iOS 7 and iOS 8:

let date = NSDate()let cal = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!// Swift 1.2:let components = cal.components(.CalendarUnitDay | .CalendarUnitMonth | .CalendarUnitYear, fromDate: date)// Swift 2:let components = cal.components([.Day , .Month, .Year ], fromDate: date)let newDate = cal.dateFromComponents(components)

(Note that I have omitted the type annotations for the variables, the Swift compilerinfers the type automatically from the expression on the right hand side ofthe assignments.)

Determining the start of the given day (midnight) can also done with the rangeOfUnit() method (iOS 7 and iOS 8):

let date = NSDate()let cal = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!var newDate : NSDate?// Swift 1.2:cal.rangeOfUnit(.CalendarUnitDay, startDate: &newDate, interval: nil, forDate: date)// Swift 2:cal.rangeOfUnit(.Day, startDate: &newDate, interval: nil, forDate: date)

If your deployment target is iOS 8 then it is even simpler:

let date = NSDate()let cal = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!let newDate = cal.startOfDayForDate(date)

Update for Swift 3 (Xcode 8):

let date = Date()let cal = Calendar(identifier: .gregorian)let newDate = cal.startOfDay(for: date)


Yes.

You don't need to fiddle with the components of the NSCalendar at all; you can simply call the dateBySettingHour method and use the ofDate parameter with your existing date.

let date: NSDate = NSDate()let cal: NSCalendar = NSCalendar(calendarIdentifier: NSGregorianCalendar)!let newDate: NSDate = cal.dateBySettingHour(0, minute: 0, second: 0, ofDate: date, options: NSCalendarOptions())!

For Swift 3:

let date: Date = Date()let cal: Calendar = Calendar(identifier: .gregorian)let newDate: Date = cal.date(bySettingHour: 0, minute: 0, second: 0, of: date)!

Then, to get your time since 1970, you can just do

let time: NSTimeInterval = newDate.timeIntervalSince1970

dateBySettingHour was introduced in OS X Mavericks (10.9) and gained iOS support with iOS 8.

Declaration in NSCalendar.h:

/*    This API returns a new NSDate object representing the date calculated by setting hour, minute, and second to a given time.    If no such time exists, the next available time is returned (which could, for example, be in a different day than the nominal target date).    The intent is to return a date on the same day as the original date argument.  This may result in a date which is earlier than the given date, of course. */- (NSDate *)dateBySettingHour:(NSInteger)h minute:(NSInteger)m second:(NSInteger)s ofDate:(NSDate *)date options:(NSCalendarOptions)opts NS_AVAILABLE(10_9, 8_0);


Here's an example of how you would do it, without using the dateBySettingHour function (to make sure your code is still compatible with iOS 7 devices):

NSDate* now = [NSDate date];NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];NSDateComponents *dateComponents = [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:now];NSDate* midnightLastNight = [gregorian dateFromComponents:dateComponents];

Yuck.

There is a reason why I prefer coding in C#...

Anyone fancy some readable code..?

DateTime midnightLastNight = DateTime.Today;

;-)