2

i have a function to convert into NSDate by using NSDateFormatter.

+(NSDate*)getDateFromCurrentTimestamp:(NSString*)currentTimestamp{
//    currentTimestamp= @"28-Feb-2012 16:42:19 PM";

    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormatter setDoesRelativeDateFormatting:NO];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
    [dateFormatter setFormatterBehavior:NSDateFormatterShortStyle];
    //[NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehaviorDefault];
    [dateFormatter setDateFormat:@"dd-MMM-yyyy HH:mm:ss a"];

    return [dateFormatter dateFromString:currentTimestamp];
}

IOS 5.x is returned correct NSDate. when run on 4.1 & 4.2 also returned correct NSDate. when run on 4.3.2 & 4.3.3, it's returned null.

how is it possible? any workaround of this?


EDIT

This is not caused by iOS bug. this is related to phone itself. some phone has different locale set in device. so we had to force it to use one locale.

HelmiB
  • 12,303
  • 5
  • 41
  • 68
  • 1
    Are your `region` and `locale` set for the 4.3.2 & 4.3.3 devices? See if [this SO question](http://stackoverflow.com/questions/1559182/how-do-you-interpret-dates-with-nsdateformatter/1559284#1559284) helps – tipycalFlow Apr 02 '12 at 04:46
  • Yeah, you probably correct. maybe the issue is related to `locale` not ios version. I'll try in a bit. – HelmiB Apr 02 '12 at 06:52
  • It worked with: `[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"] autorelease]];`. thanks. too bad can't accept your answer. – HelmiB Apr 02 '12 at 07:24

2 Answers2

1

It looks like someone ran into this issue before. A workaround is written up here: NSDateFormatter is returning null for date in ios 4.3.3

Community
  • 1
  • 1
ThomasW
  • 16,981
  • 4
  • 79
  • 106
1

From our discussion in the comments:

Are your region and locale set for the 4.3.2 & 4.3.3 devices? See if this SO question helps.

Relevant code:

[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"] autorelease]];
Community
  • 1
  • 1
tipycalFlow
  • 7,594
  • 4
  • 34
  • 45