iosdatetimeios5

In iOS, how to save the current hour, minutes and seconds from current time into separate ints?


In iOS, is it possible to save hours, minutes, seconds into different integer variables from the current time?


Solution

  • Yes, you can:

    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:[NSDate date]];
    NSInteger hour = [components hour];
    NSInteger minutes = [components minute];
    NSInteger seconds = [components second];