How to Get time difference in iPhone

Your problem has two parts, parsing the time and getting the difference: NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; [dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@”en_US_POSIX”] autorelease]]; [dateFormatter setDateFormat:@”mm:ss:SS”]; NSDate* firstDate = [dateFormatter dateFromString:@”01:00:00″]; NSDate* secondDate = [dateFormatter dateFromString:@”01:02:00″]; NSTimeInterval timeDifference = [secondDate timeIntervalSinceDate:firstDate];

NSDateFormatter and yyyy-MM-dd

You are facing this problem because your date formatter is not correct.Suppose your newInvoice.date variable store “11:02:23″ the your dateFormatter should be @”yy:MM:dd” and if your newInvoice.date variable store”2/22/11″ then your dateFormatter should be @”MM/dd/yy” NSDate *dateTemp = [[NSDate alloc] init]; NSDateFormatter *dateFormat1 = [[NSDateFormatter alloc] init]; NSDateFormatter *dateFormat2 = [[NSDateFormatter alloc] init]; [dateFormat1 setDateFormat:@”MM/dd/yy”]; … Read more

Why NSDateFormatter can not parse date from ISO 8601 format [duplicate]

The problem is with the timezone on the end. You need to either have it as: GMT-0X:00 or as -0X00 with no separate between hours and minutes. The following two combinations work: Combo 1 – use GMT format (GMT-0X:00) and ZZZZ NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; [dateFormatter setDateFormat:@”yyyy-MM-dd’T’HH:mm:ssZZZZ”]; NSLog(@”DATE FORMAT:%@”, [dateFormatter dateFromString:@”2008-12-29T00:27:42GMT-08:00″]); Combo … Read more

Get current iPhone device timezone date and time from UTC-5 timezone date and time iPhone app?

I have tested your scenario and added some code for your reference. Please test the below and please let me know it is useful for you. NSString *dateStr = @”2012-07-16 07:33:01″; NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init]; [dateFormatter1 setDateFormat:@”yyyy-MM-dd HH:mm:ss”]; NSDate *date = [dateFormatter1 dateFromString:dateStr]; NSLog(@”date : %@”,date); NSTimeZone *currentTimeZone = [NSTimeZone localTimeZone]; NSTimeZone *utcTimeZone … Read more

Difference between two NSDate objects — Result also a NSDate

NSDate represents an instance in time, so it doesn’t make sense to represent an interval of time as an NSDate. What you want is NSDateComponents: NSDate *dateA; NSDate *dateB; NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:dateA toDate:dateB options:0]; NSLog(@”Difference in date components: %i/%i/%i”, components.day, components.month, components.year);