5

I have 2 times values in NSString either in 12 hour format(10-09 PM) or 24 hour format (18-12). How to calculate time difference between two? (two dates will be either in 12 hour or 24 hour format, not mixed format)

sach
  • 1,069
  • 4
  • 15
  • 29
  • possible duplicate of [How to Get time difference in iPhone](http://stackoverflow.com/questions/1427151/how-to-get-time-difference-in-iphone) – Brad Larson Apr 17 '12 at 14:59

1 Answers1

12
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];

hope this helps

visakh7
  • 26,380
  • 8
  • 55
  • 69