0

I know there is a lot of threads on this subject and I have read them all.

In my particular case when tested on the iPhone 6.1 Simulator dateFromString is working flawlessly but returns null on iPhone 5.1 Simulator. I have also tested it on several devices with iOS6 and iOS5 getting to the same conclusion.

The format in 'input' being: 2013-02-15T18:00:00+03:00

I wrote the following code:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
[dateFormatter setLocale:enUSPOSIXLocale];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss+zz:zz"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:input];

[dateFormatter setDateFormat:@"EEE'. 'dd' 'MMM' 'yyyy', 'HH':'mm"];
NSString *stringFromDate = [dateFormatter stringFromDate:dateFromString];
return stringFromDate;

... to obtain in stringFromDate "Fri. 15 Feb 2013, 18:00" on iOS6 and "null" on iOS5 on both simulators and devices.

Please help me

n2oula
  • 26
  • 1
  • Why are you setting the `[dateFormatter setDateFormat:...]` twice? – flashfabrixx Feb 06 '13 at 12:00
  • I am using the same dateFormatter to change the format to the new one after converting the input String into a NSDate – n2oula Feb 06 '13 at 12:15
  • Oh, okay. After some research I've found a possible like that deals with changes to the location the user (or the simulator simulates) is located. http://developer.apple.com/library/ios/#qa/qa1480/_index.html – flashfabrixx Feb 06 '13 at 18:23
  • Thanks for the link! Unfortunately I have tried all what's said there, I still get the same result... PS: Both 6.1 and 5.1 simulators are on the same XCode installation (hence both have the same system Time and Date settings) – n2oula Feb 07 '13 at 11:52
  • Any clue? I'm still stuck – n2oula Feb 11 '13 at 15:31
  • Seems to a problem with the `:` in the formatter. Check this question and the answer in the first comment. http://stackoverflow.com/questions/11413600/nsdateformatter-return-nil-in-ios-5 – flashfabrixx Feb 12 '13 at 16:27
  • 1
    Thank you, this thread lead me to another one which helped me: I was able to solve it by using the following lines of code: NSDate *theDate = nil; NSError *error = nil; if (![dateFormatter getObjectValue:&theDate forString:input range:nil error:&error]) {} – n2oula Feb 13 '13 at 11:05

1 Answers1

1

It's not iOS5 or iOS 6 issue. Please check about time format set in setting whether it is 12-hour format or 24-hour format.

It will solve your problem

Scorpian Alive
  • 734
  • 8
  • 21
  • Dear Vijay, Thank you for your quick reply. I have tried it on a device, it still returns me null on iOS5, and concerning the simulators I think the Mac OS X system settings are taken into account; there is no Date & Time option in the General pane of the Settings. – n2oula Feb 06 '13 at 12:13