如何获得像ios中谷歌地图持续时间文本格式的持续时间?

时间:2018-04-27 07:25:08

标签: ios objective-c swift mapbox

我必须将第二个转换为类似的内容。

let insertRows = [];
items.forEach(item => {
    insertRows.push([
        "INSERT INTO items (id, type, date, message) VALUES (?, ?, ?, ?)",
        [item.id, item.type, item.date, item.message]
    ]);
});
this.database.sqlBatch(insertRows).then((result) => {
    console.info("Inserted items");
}).catch(e => console.log(e));

我正在使用mapbox并将json作为响应,其中持续时间以秒为单位。就像这样:

var item = new CreateOrderRequestLineItem()
            {
                //Name = "Yaar Book",
                //Note = "New Book",
                CatalogObjectId = "STWMISUMLIIIXU6MVWNFX6FQ",
                Quantity = "1",
                BasePriceMoney = new Money() { Amount = 20, Currency = Money.CurrencyEnum.NPR }
            };

2 个答案:

答案 0 :(得分:2)

试试这个。

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);

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

答案 1 :(得分:1)

我很欣赏以上答案。但我看起来像下面的东西。它解决了我的问题。

//convert to minute
    int minutes = [seconds doubleValue] / 60;

    //Convert to hours
    int hours = [seconds doubleValue] / (60 * 60);

    //Convert to days
    int days = [seconds doubleValue] / (60 * 60 * 24);

    if (days>=1)
    {
        return [NSString stringWithFormat:@"%d %@",days,days==1?@"day":@"days"];
    }
    else if (hours>=1)
    {
        return [NSString stringWithFormat:@"%d hours %d min",hours,minutes];
    }
    else
    {
        return [NSString stringWithFormat:@"%d min",minutes];
    }