Setting NSCalendarDate Time

Discussion in 'Mac Programming' started by stadidas, Jan 14, 2007.

  1. macrumors regular

    Feb 27, 2006
    Kent, United Kingdom

    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.
  2. macrumors 6502

    Jul 25, 2006
    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.

Share This Page