Calculating the time difference in Objective-C

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

+4
source share
1 answer
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

+10
source

All Articles