-2

I want to find the time (in minutes) between a date with the "yyyy-MM-dd'T'HH:mm:ss" format (Sample:"2014-02-03T11:28:00") and current time (In GMT, as the string is in GMT. I tried

 NSDateFormatter *formatter;
[formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];

NSDate *now = [NSDate date];
NSDate *trackingDataDate = [formatter dateFromString:timeStamp];

NSTimeInterval distance = [now timeIntervalSinceDate:trackingDataDate];
double timeInMinutes = distance / 60;

But this returns NaN. What is wrong with my code, how can I get the time between the events?

Oscar Apeland
  • 6,422
  • 7
  • 44
  • 92

2 Answers2

1

Simple mistake. You have miss allocation for NSDateFormatter. Add below line instead of NSDateFormatter *formatter;, It's working fine.

NSDateFormatter *formatter = [[NSDateFormatter alloc]init];

Update: According to your comment, see my below code for convert to GMT Date.

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss";

NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
[dateFormatter setTimeZone:gmt];
NSString *timeStamp = [dateFormatter stringFromDate:[NSDate date]];
Mani
  • 17,549
  • 13
  • 79
  • 100
0

Try this:

NSString* string=@"26-01-2014";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-mm-yyyy"];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
[dateFormatter setTimeZone:gmt];

NSDate *date = [dateFormatter dateFromString:string];
NSLog(@"Date = %@",date);
Rajesh Loganathan
  • 11,129
  • 4
  • 78
  • 90