我在NSString中有2次以12小时格式(10-09 PM)或24小时格式(18-12)的值。如何计算两者之间的时差? (两个日期将是12小时或24小时格式,而不是混合格式)
答案 0 :(得分:11)
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];
希望这会有所帮助