NSCalendarDate

Discussion in 'iOS Programming' started by newdev, Aug 18, 2008.

  1. newdev macrumors newbie

    Joined:
    Aug 5, 2008
    #1
    I need help with NSCalendarDate....

    How do I convert the following functionality to iPhone app?

    PHP:
        NSCalendarDate *myDate = [[NSCalendarDate calendarDate]
                                    
    dateByAddingYears:0
                                    months
    :0
                                    days
    :0
                                    hours
    :-[[NSCalendarDate calendarDatehourOfDay]
                                    
    minutes:-[[NSCalendarDate calendarDateminuteOfHour]
                                    
    seconds:-[[NSCalendarDate calendarDatesecondOfMinute]];
        
        
    int hours 12;
        
    int minutes floor(hours 60);
        
        return [
    myDate dateByAddingYears:0 months:0 days:0 hours:hours minutes:minutes seconds:0];
    any ideas?
     
  2. grimjim macrumors member

    Joined:
    May 24, 2003
    #2
    Well, NSCalendarDate has been deprecated in favour of NSDate, so I suggest you look up the NSDate class reference in the documentation, and see how much of it has changed. You might also want to read 'Date and Time Programming Guide for Cocoa' which is also available on the documentation. I suspect that you won't need to change very much, just start using NSDate objects instead of NSCalendarDate.
     
  3. jguru macrumors member

    Joined:
    Apr 10, 2008
    #3
    I have been struggling with the same issue. I will let u know once I find a solution.. there isn't a 1-1 replacement of all the functionality of NSCalendarDate in NSDate .. :(
     
  4. admanimal macrumors 68040

    Joined:
    Apr 22, 2005
    #4
    You need a combination of NSDate, NSDateComponents, and NSCalendar. It's pretty easy once you figure them out.
     
  5. jguru macrumors member

    Joined:
    Apr 10, 2008
    #5
    do you have any small example that can show how to replace the above code? it will be of great help :)

    thanks
     
  6. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    I'm trying to work out what the code is trying to do. What is the output date?

    It seems to take the current date and time, delete the hours, minutes, seconds (so getting back to midnight?). Then it adds 12 hours and 12*60 minutes so gets to the start of tomorrow?
     
  7. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #7
    Assuming my interpretation is correct:

    Code:
    NSDate *now = [[NSDate alloc] init];
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *comps = [cal components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:now];
    
    [comps setHour:-[comps hour]];
    [comps setMinute:-[comps minute]];
    [comps setSecond:-[comps second]];
    
    NSDate *startOfToday = [cal dateByAddingComponents:comps toDate:now options:0]; //This variable should now be pointing at a date object that is the start of today (midnight);
    
    [comps setHour:24];
    [comps setMinute:0];
    [comps setSecond:0];
    
    NSDate *startOfTomorrow = [cal dateByAddingComponents:comps toDate: startOfToday options:0];
    
    Note this code is untested. I've not even compiled it: I just typed it straight into here. Assume there are typos and errors.

    This all seems pretty simple and clear. Have you tried reading the documentation?
     
  8. jguru macrumors member

    Joined:
    Apr 10, 2008
    #8
    ^ thanks.. i am not sure what newdev was trying to do but your example helps me a lot.. :) cheers
     

Share This Page