-2

This NSDate able to get the show when i debugging in simulator from xcode. But when I debugging in iphone6 it show NULL.

NSString *dateWithTime = [NSString stringWithFormat:@"2019/03/18 09:30 AM"];

    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = @"yyyy-MM-dd hh:mm a";
    [fmt setAMSymbol:@"am"];
    [fmt setPMSymbol:@"pm"];

    fmt.timeZone = [NSTimeZone systemTimeZone];
    NSDate *localDate = [fmt dateFromString:dateWithTime];
    NSLog(@"DATE -- %@ ", [fmt stringFromDate:localDate]);

No idea is it because the phone timezone not using am/pm or any other causes.

Kindly please help me answer it.

joey
  • 73
  • 2
  • 14
  • 3
    What is the exact value of `dateWithTime`? Please put that in your question. – rmaddy Mar 12 '19 at 03:12
  • already edit the question. the date i stated in the dateWithTime. – joey Mar 12 '19 at 03:20
  • 1
    Start by changing the `-` to `/` to match the string. Try changing `am` to `AM` to match the string. If none of those work, set the formatter's locale to the special locale of `en_US_POSIX` (which you should do either way). – rmaddy Mar 12 '19 at 03:24
  • thanks you so much. By setting thing locale does work. – joey Mar 12 '19 at 04:04

1 Answers1

2

Try setting the locale for the date formatter

fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];

Update

For en_US_POSIX locale you can use:

 fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
Pooja Kamath
  • 1,290
  • 1
  • 10
  • 17