0

I am trying to work through some of the AppsAmuck tutorials but they mostly seem to use code that Apple has deprecated. For example, there is a function that is called by a timer every second that looks like this:

NSDate *now = [NSDate date];
    int hour = 23 - [[now dateWithCalendarFormat:nil timeZone:nil] hourOfDay];
    int min = 59 - [[now dateWithCalendarFormat:nil timeZone:nil] minuteOfHour];
    int sec = 59 - [[now dateWithCalendarFormat:nil timeZone:nil] secondOfMinute];
    countdownLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hour, min,sec];

Unfortunately, none of this works with today's current SDK. I have been staring glossy-eyed at Apple's NSDate documentation and I am having trouble translating the intent of the function above into current day NSDate lingo.

startuprob
  • 1,917
  • 5
  • 30
  • 45
  • 1
    This exactly same question related to AppsAmuch was asked before and has several helpful answers - [How do I get the current date in Cocoa](http://stackoverflow.com/questions/1070354/how-do-i-get-the-current-date-in-cocoa) – Anurag Oct 17 '10 at 00:56

1 Answers1

0

This is what I use...but there are other methods

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponents = [gregorian components:(NSHourCalendarUnit  | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:[NSDate date]];
NSInteger hour = [dateComponents hour];
NSInteger minute = [dateComponents minute];
NSInteger second = [dateComponents second];
[gregorian release];

Returns the hour, minute and second from the current

Jordan
  • 21,746
  • 10
  • 51
  • 63