计算目标C中的时间差异

我在NSString中使用12小时格式(10-09 PM)或24小时格式(18-12)有2次值。 如何计算两者之间的时间差? (两个date将是12小时或24小时格式,不是混合格式)

NSDateFormatter *df=[[NSDateFormatter alloc] init]; // Set the date format according to your needs [df setDateFormat:@"MM/dd/YYYY hh:mm a"]; //for 12 hour format //[df setDateFormat:@"MM/dd/YYYY HH:mm "] // for 24 hour format NSDate *date1 = [df dateFromString:firstDateString]; NSDate *date2 = [df dateFromString:secondDatestring]; NSLog(@"%@f is the time difference",[date2 timeIntervalSinceDate:date1]); [df release]; 

希望这可以帮助