1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Setting NSCalendarDate Time

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

  1. macrumors regular

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

    #2
    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.
     

Share This Page