-1

I have my data in JSON format which contains some date:

"NOME_PUBBL_LINEA" = "FOLLONICA - PIOMBINO";
    "ORA_ARRIVA" = "1899-12-30T06:45:00";
    "ORA_PARTE" = "1899-12-30T05:50:00";

I want to convert it like :

 ora_arriva = 6.45,
   ora_parte = 5.50

I have tried following :

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"YYYY-MM-dd'T'HH:mm:ss'+02:00'"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"Italy/Rome"]];
NSDate *dte = [dateFormat dateFromString:dataDic[@"ORA_ARRIVA"]];

But it is giving me like

1899-12-30 00:51:40 +0000

for ora_arriva.

Larme
  • 24,190
  • 6
  • 51
  • 81
vivek bhoraniya
  • 1,526
  • 2
  • 17
  • 36

3 Answers3

0
[dateFormat setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'+02:00'"];

Just yyyy :)

StrawHara
  • 1,301
  • 19
  • 33
0

dateFromString returns an NSDate. You need take that NSDate object and construct your time:

NSDate *myDate = [NSDate date];

NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *comp = [gregorianCalendar components:NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit fromDate:myDate];

NSLog(@"current time = %ld:%ld", [comp hour], [comp minute]);

Replace the myDate with your actual NSDate you got back from your dateFormatter

Zhang
  • 11,549
  • 7
  • 57
  • 87
  • The issue is that the time is wrong, not that the OP is unable to extract hour/minute from the parsed date. – trojanfoe Jun 24 '14 at 09:21
0

You need something like this

[dateformatter setDateFormat:@"MM-dd-yyyy hh:mm:ss"];
NSString *dateString = [dateformatter stringFromDate:adate];
yoshiiiiiiii
  • 953
  • 8
  • 20