Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert NSTimeInterval to NSString in iPhone?

How to covert the NSTimeInterval to NSString? I have

NSTimeInterval  today = [[NSDate date] timeIntervalSince1970]; 

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

like image 366
Warrior Avatar asked Nov 25 '10 14:11

Warrior


2 Answers

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]; 
like image 140
Vladimir Avatar answered Sep 21 '22 10:09

Vladimir


-(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; } 
like image 37
Ofir Malachi Avatar answered Sep 19 '22 10:09

Ofir Malachi