Search code examples
iosobjective-cnsdatensdateformatter

Wrong NSDate conversion from String


I have date(NSDate),

I convert with format “EEEE,dd MMM YYYY” so output is. “Monday,22 May 2017”

But when convert this string to date it give wrong date

using this code

 NSDateFormatter * formatter = [NSDateFormatter new];
 [formatter setDateFormat:@"EEEE,dd MMM YYYY"];
  NSDate *currentDate = [formatter dateFromString:“Monday,22 May 2017”];

Wrong output is 2016-12-25 18:30:00 +0000

anyone can help please?


Solution

  • Date has no proper format, but in here use yyyy instead of YYYY

    @"YYYY" is week-based calendar year.

    @"yyyy" is ordinary calendar year.

    [formatter setDateFormat:@"EEEE,dd MMM yyyy"];
    

    for sample

    NSDateFormatter * formatter = [NSDateFormatter new];
    [formatter setDateFormat:@"EEEE,dd MMM yyyy"];
    NSDate *currentDate = [formatter dateFromString:@"Monday,22 May 2017"];