-2

I'm trying to convert a date/time from US to GMT. It does not seem to work probably. It keep returning even null.

int subObjects = ((NSArray *)jsonResult[@"match"]).count;
for (int i = 0; i <= subObjects-1; i++) {


    NSString *date = [NSString stringWithFormat:@"%@ %@",[[[jsonResult valueForKey:@"match"] valueForKey:@"playdate"] objectAtIndex:i], [[[jsonResult valueForKey:@"match"] valueForKey:@"time"] objectAtIndex:i]];


    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setTimeZone: [NSTimeZone timeZoneWithName:@"US/Arizona"]];
    [df setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
    [df setLocale:[NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]];
    NSDate *myDate = [df dateFromString:[NSString stringWithFormat:@"%@", date]];

}

myDate returns (null).

i've checked my date string and it returns this, so it is not that which is the problem. I've searched on stackoverflow and this is what i've done. What am i doing wrong?

date log

2014-03-16 09:00:00
2014-03-08 09:00:00
2014-03-09 09:00:00
2014-03-15 10:00:00
2014-03-09 11:00:00
2014-03-08 11:00:00
2014-03-16 11:00:00
2014-03-10 12:00:00
2014-03-15 12:00:00

myDate log

 2014-03-16 16:00:00 +0000
 2014-03-08 16:00:00 +0000
 2014-03-09 16:00:00 +0000
 2014-03-15 17:00:00 +0000
 2014-03-09 18:00:00 +0000
 2014-03-08 18:00:00 +0000
 2014-03-16 18:00:00 +0000
 2014-03-10 07:00:00 +0000
 2014-03-15 07:00:00 +0000
 (null)
user3258468
  • 354
  • 7
  • 18

1 Answers1

2

The date format for the 24-hour time is HH, not hh. And, as observed e.g. in What is the best way to deal with the NSDateFormatter locale "feechur"?, you should add

[df setLocale:[NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]];

to be independent of the region settings on the device.

Community
  • 1
  • 1
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • region doesnt apply here - checked – Daij-Djan Mar 08 '14 at 20:34
  • @Daij-Djan: We'll see. The question was edited while I prepared the answer (so that the code does not match the output anymore). – Martin R Mar 08 '14 at 20:36
  • after adding your code i seem to get a strange response. the first 7 is correct, but the last 2 is not and then there is one returning 0. Can this be explained? you can see what it returns in my question – user3258468 Mar 08 '14 at 20:50
  • @user3258468: That looks as if you did not replace "hh" by "HH". – Martin R Mar 08 '14 at 20:56