Convert NSTimeInterval to NSString in iPhone?

蓝咒 提交于 2019-12-03 07:33:29

问题


How to covert the NSTimeInterval to NSString? I have

NSTimeInterval  today = [[NSDate date] timeIntervalSince1970];

I have to give "today" as input as NSString.


回答1:


NSTimeInterval is just a double type so you can convert it to string using +stringWithFormat: method

NSTimeInterval  today = [[NSDate date] timeIntervalSince1970];
NSString *intervalString = [NSString stringWithFormat:@"%f", today];



回答2:


-(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;
}


来源:https://stackoverflow.com/questions/4278129/convert-nstimeinterval-to-nsstring-in-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!