View Full Version : Setting NSCalendarDate Time

Jan 14, 2007, 07:46 PM

In one of my programs I have an instance of NSCalendarDate initialised to the current date and time. I wanted to know if there is a way of initialising it to the current date, but with the time set to midnight. If not, is there a way to set the time to midnight after it has been initialised? Thanks to anyone who can help.

Jan 15, 2007, 01:06 AM
NSCalendarDates are immutable, so there's no way to change them after they've been initialized. Two NSCalendar dates are necessary, as follows:

NSCalendarDate *currentDate = [NSCalendarDate currentDate];
NSCalendarDate *todayMidnight = [NSCalendarDate
dateWithYear: [currentDate yearOfCommonEra]
month: [currentDate monthOfYear]
day: [currentDate dayOfMonth]
hour: 0
minute: 0
second: 0
timeZone: nil];

Remember to retain as necessary.