Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

stadidas

macrumors regular
Original poster
Feb 27, 2006
243
0
Kent, United Kingdom
Hi.

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.
 

kpua

macrumors 6502
Jul 25, 2006
294
0
NSCalendarDates are immutable, so there's no way to change them after they've been initialized. Two NSCalendar dates are necessary, as follows:
Code:
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.