Bug when trying to parse date in Objective-C

后端 未结 2 561
悲哀的现实
悲哀的现实 2020-12-11 12:44

When I was trying to convert April from 04 (MM) to Apr (MMM) it turned back into nil, is this a known issue in iOS or am I doing somet

2条回答
  •  醉话见心
    2020-12-11 13:28

    I tried your code and it gives me null for year 2022, but years 2018,2020 it gives Apr ... weird!

        NSString *dateString=@"2022-04-01";
    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSDate* myDate = [dateFormatter dateFromString:dateString];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"+5:30"]];
    [dateFormatter setDateFormat:@"MMM"];
    NSString *stringFromDate = [dateFormatter stringFromDate:myDate];
    NSLog(@"%@ == %@",dateString,stringFromDate);
    

    UPDATE:

    Please parse the string as this : 2016-04-01 01:00

    Because of daylight saving in Jordan the clock will be shifted +1 hour so there's no time in the universe like this: 2016-04-01 00:00

    This should resolve ur issues with the nil object.

提交回复
热议问题