我得把第二个转换成下面的内容。

50 minutes
1 hour 40 minutes
1 day 2 hours 20 minutes

我正在使用mapbox并获取json作为响应,其中持续时间以秒为单位。如下所示:
duration:2263.1

最佳答案

试试这个。

int time = timeFromApiInSeconds;
int minutes = (time / 60) % 60;
int hours = time / 3600;

    if (minutes == 0 && hours == 0){
       NSString *lblStr = [NSString stringWithFormat:@"%02d Seconds", time];
    }

    if (hours == 0){
         NSString *lblStr = [NSString stringWithFormat:@"%02d Minutes %02d Seconds", minutes,time];

    }

所以。。
另一个办法是。
   NSDate *startDate = [NSDate date];
    NSDate *endDate = [NSDate dateWithTimeInterval:(secondsFromApi) sinceDate:startDate];

    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSGregorianCalendar];

    NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
    NSDateComponents *components = [gregorian components:unitFlags
                                                fromDate:startDate
                                                  toDate:endDate options:0];
    NSInteger years = [components year];
    NSInteger months = [components month];
    NSInteger days = [components day];
    NSInteger minuts = [components minute];
    NSLog(@"years = %ld months = %ld days = %ld = %02d Minutes",years,months,days, minuts);

即使你也可以在组件中获得秒数。

关于ios - 如何在iOS中获取像谷歌 map 持续时间文本格式这样的持续时间?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50057400/

10-12 04:47