Troy Barrios
Troy Barrios

Reputation: 177

Converting string to date and finding time difference

I want to convert a string to a date time object and then subtract the difference between 2 dates. However I keep getting an invalid pointer warning. The date is formatted 20131209 02:34. My code is below

NSDateFormatter *df =[[NSDateFormatter alloc]init];
[df setDateFormat:@"YYYYMMDD HH:MM"];
NSDate *currTime = [df dateFromString:self.currentTime];
NSDate *predicTime =[df dateFromString:self.predictedTime];
NSTimeInterval newtime=[predicTime timeIntervalSinceDate:currTime];

Upvotes: 0

Views: 116

Answers (2)

Maulik
Maulik

Reputation: 19418

Your date format was incorrect [df setDateFormat:@"YYYYMMDD HH:MM"]; I assumed that your strings are valid !

Try :

NSDateFormatter *df =[[NSDateFormatter alloc]init];
[df setDateFormat:@"yyyyMMdd HH:mm"];  // 24hr format, for 12hr format use 'hh'
NSDate *currTime = [df dateFromString:@"20131209 02:34"];
NSDate *predicTime =[df dateFromString:@"20131209 04:34"];
NSTimeInterval newtime=[predicTime timeIntervalSinceDate:currTime];

Upvotes: 1

Suhit Patil
Suhit Patil

Reputation: 12023

try out this code

 NSDateFormatter *df =[[NSDateFormatter alloc]init];
 [df setDateFormat:@"yyyyMMdd hh:mm"];
 NSDate *currTime = [df dateFromString:self.currentTime];
 NSDate *predicTime =[df dateFromString:self.predictedTime];
 NSTimeInterval newtime=[predicTime timeIntervalSinceDate:currTime];

Upvotes: 0

Related Questions