[Code Note] iOS-computing time difference, note ios-Time Difference
I. Code.
-(Void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. // time format: 18:34:31 NSLog (@ "% ld", [self getTimeInterval: @ "00:00:00"]);} // calculate the time difference-(NSInteger) getTimeInterval :( NSString *) sendDateString {NSInteger minute; if (sendDateString = nil | sendDateString. length = 0) {;} else {NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat: @ "yyyy-MM-dd HH: mm: ss "]; // current time NSDate * currentdate = [NSDate date]; NSDate * currentDate = [dateFormatter dateFromString: [dateFormatter stringFromDate: currentdate]; NSLog (@ "--- currentdate -- % @", currentDate); // End Time NSDate * endDate = [dateFormatter dateFromString: sendDateString]; NSLog (@ "--- endDate -- % @", endDate); // get the time difference NSTimeInterval time = [currentDate timeIntervalSinceDate: endDate]; NSLog (@ "--- time --- % ld", (long) time ); NSLog (@ "-- minitus -- % ld", (long) time); NSLog (@ "--- days -- % d", (int) time) /(3600*24); NSLog (@ "--- hours -- % d", (int) time) % (3600*24)/3600);} return minute ;}
2. output.
10:14:12. 467 calculation time difference [1519: 59582] --- currentdate -- 02:14:12 + 00002015-10-23 10:14:12. 467 calculation time difference [1519: 59582] --- endDate -- 16:00:00 + 00002015-10-23 10:14:12. 467 calculation time difference [1519: 59582] --- time --- 82448522015-10-23 10:14:12. 467 calculation time difference [1519: 59582] -- minitus -- 82448522015-10-23 10:14:12. 467 calculation time difference [1519: 59582] --- days -- 952015-10-23 10:14:12. 468 calculation time difference [1519: 59582] --- hours -- 102015-10-23 10:14:12. 468 calculation time difference [1519: 59582] 0