1

In some scenario, I am getting currentDateString as nil.My app is in live so i can't find out exact scenario.

 NSDate *currentDate = [NSDate date];
 NSCalendar *myCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
 [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
 NSString *currentDateString =  [dateFormatter stringFromDate:currentDate];
divya d
  • 169
  • 7

1 Answers1

0

I tried this code. I got correct results. Please set DateFormatter Locale

NSDate *currentDate = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSString *currentDateString =  [dateFormatter stringFromDate:currentDate];
Lal Krishna
  • 15,485
  • 6
  • 64
  • 84