Search code examples
objective-cnsdatecountdowncountdowntimer

How can I get the amound of seconds from a NSDate ("hh:mm:ss") and vice versa


I am trying to make a timer and yes I have looked at NSTimer and it is not what I'm looking for, I basically need a way to convert a NSDate in the format of ("hh:mm:ss") into the amount of seconds that would be in this. And then I need a way to do the opposite so convert the seconds to the amount of hours, minutes and leftover seconds from that.


Solution

  • You can extract components from NSDate using the NSCalendar class. The following example extracts a single component, NSCalendarUnitSecond, from the current date.

    NSDate *today = [NSDate date];
    NSCalendar *localCalendar = [NSCalendar currentCalendar];
    NSUInteger seconds = [localCalendar component:NSCalendarUnitSecond fromDate:today];