0

Ive been racking my brains with no luck. Could someone please tell my how i would convert this string:

 NSString *dateTimeStr =[[self.responseArray objectAtIndex:indexPath.row]objectForKey:@"date_time"];

2014-04-13T17:00:00+11:0

into a NSDate? and time format ?

how can i covert this date into time format ?

here is my code:

NSString *dateTimeStr =[[self.responseArray objectAtIndex:indexPath.row]objectForKey:@"date_time"];

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

[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];

[dateFormatter setDateFormat:@"yyyy-MM-ddHH:mm:ss"];

NSDate *date = [dateFormatter dateFromString:dateTimeStr];

NSLog(@"date %@", date);

NSTimeInterval seconds = [date timeIntervalSinceReferenceDate];

in console

NSLog(@"seconds==> %f", seconds);

i have time in miliseconds then how can i take out minutes/hours/ ?

Bhanu
  • 1,249
  • 10
  • 17
zeeshan shaikh
  • 819
  • 3
  • 18
  • 33
  • possible duplicate of http://stackoverflow.com/questions/5559912/date-time-parsing-in-ios-how-to-deal-or-not-deal-with-timezones – RobP Apr 09 '14 at 04:47
  • you mean you need to convert string to date, instead of date to timeformat. – Anoop Vaidya Apr 09 '14 at 04:58
  • @AnoopVaidya i need to convert my string to timeFormat, actually i want to take difference between my current time and json response nsstring so i can get the result in mins/hours like this 1hour ago – zeeshan shaikh Apr 09 '14 at 05:03
  • @AnoopVaidya i want to take sec/min/hour from that string. – zeeshan shaikh Apr 09 '14 at 05:09

1 Answers1

3
NSString *dateString = @"2014-04-13T17:00:00+11:00";
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];

NSDate *date = [dateFormatter dateFromString:dateString];

NSDateComponents *dateComponents = [[NSCalendar currentCalendar]
                                    components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit
                                    fromDate:date];

NSInteger year = [dateComponents year];
NSInteger month = [dateComponents month];
NSInteger day = [dateComponents day];
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140