NSDate returning nil for the NSDateFormatter set c

2019-09-18 20:38发布

问题:

I am getting the date in NSString format as mentioned below.

Input:datestring = 30/04/2013 04:49 PM

I am applying the format for NSDateformatter as mentioned below to convert this NSString to NSDate .

Format:dateFormat= dd/MM/yyyy hh:mm a

I still get the date conversion to Nil. Please help as I am not understanding this issue.Here is my code snippet to explain what i am doing in the utility function.

  NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  [dateFormatter setDateFormat:dateFormat];
  NSDate *date = [dateFormatter dateFromString:dateString];
  return date;

回答1:

Try This....

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd/MM/yyyy hh:mm a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
NSDate *date = [dateFormatter dateFromString:@"30/04/2013 04:49 PM"];
NSLog(@"%@",date);