Search code examples
iosobjective-cnsdateformatter

I have written code for chat application, works fine for iOS 12 but in iOS 13, it return nil value


enter image description hereI have written code for chat application,works fine for iOS 12 ,and in iOS 13, it return nil value.

this is my code snippet for dateformatter.

-(NSDate *)stringToDateNew:(NSString*)strDate withForamt:(NSString *)strFormat {
   @try
   {
       NSDate *myDate;

       if(strDate!=NULL)
       {
           NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
           [dateFormat setDateFormat:[NSString stringWithFormat:@"%@",strFormat]];
           NSLocale * enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] ;
           if (enUSPOSIXLocale == nil) {
               enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
           }
           else
           {
            [dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
            [dateFormat setLocale:enUSPOSIXLocale];
            myDate = [dateFormat dateFromString:strDate];
            return myDate;
           }

       }
       return NULL;

   }
   @catch (NSException *exception)
   {
       NSLog(@"Excption in AppDelegate : stringToDate %@:%@",exception.name,exception.reason);
   }
}

Solution

  • use the withformat "yyyy-mm-dd'T'hh:mm:ss.SSS".It Will work for os 13 as well.