Convert NSTimeInterval to NSString in iPhone?

后端 未结 2 711
广开言路
广开言路 2021-02-02 08:22

How to covert the NSTimeInterval to NSString? I have

NSTimeInterval  today = [[NSDate date] timeIntervalSince1970];

I have to give \"today\" a

2条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-02 09:27

    -(NSString*)formattedDuration:(NSTimeInterval)interval{
        NSDateComponentsFormatter *formatter = [[NSDateComponentsFormatter alloc] init];
        formatter.allowedUnits = NSCalendarUnitHour | NSCalendarUnitMinute;
        formatter.zeroFormattingBehavior = NSDateComponentsFormatterZeroFormattingBehaviorPad;
        NSString *string = [formatter stringFromTimeInterval:interval];
        NSLog(@"%@", string);
        // output: 0:20:34
        return string;
    }
    

提交回复
热议问题