NSTimeInterval Formatting

后端 未结 5 923
梦谈多话
梦谈多话 2020-12-02 19:51

I want to take my NSTimeInterval and format it into a string as 00:00:00 (hours, minutes, seconds). What is the best way to do this?

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-02 20:46

    NSTimeInterval interval = ...;    
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval];    
    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormatter setDateFormat:@"HH:mm:ss"];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
    NSString *formattedDate = [dateFormatter stringFromDate:date];
    NSLog(@"hh:mm:ss %@", formattedDate);
    

提交回复
热议问题