Search code examples
iphoneobjective-cunix-timestamp

Converting UNIX timestamp into date, hours, minutes, seconds


How can you convert the UNIX timestamp (that is in the form of a number denoting timestamp) into date, time(hours, minutes, seconds) in Objective-C?


Solution

  • For output, use an NSDateFormatter. To extract values for other purposes, use NSDateComponents. eg:

    NSDate *date = [NSDate dateWithTimeIntervalSince1970:1234567];
    NSDateComponents *weekdayComponents = [[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:date];
    int weekday = [weekdayComponents weekday];