Reputation: 19
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 something wrong?
Here's my code for inspection:
NSString *dateString=@"2016-04-01";
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSDate* myDate = [dateFormatter dateFromString:dateString];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MMM"];
NSString *stringFromDate = [formatter stringFromDate:myDate];
NSLog(@"%@ == %@",dateString,stringFromDate);
when change dataString=@"2015-04-01"
the stringFromDate
returns Apr
This is my output:
2016-04-01 12:17:45.708[32172:2389414] 2016-04-01 == (null)
2016-04-01 12:17:47.446[32172:2389414] 2015-04-01 == Apr
Upvotes: 1
Views: 742
Reputation: 130092
This is probably a problem in your time zone. Note that when parsing a date without time, the time is assumed to be zero. However, some specific times don't exist, usually due to Daylight Saving Time changes (which happen in March around the world) or due to specific local time changes.
For example, in Jordan, the DST change is Apr 1 2016, 00:00 => Apr 1 2016, 01:00
. That means that the time Apr 1 2016, 00:00
does not exist because March 31 2016, 23:59
becomes immediately Apr 1 2016, 01:00
. If the date does not exist, date formatter must return nil
.
You can usually fix the problem by using a preset GMT time zone that does not have this problem, e.g
dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
or
dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:[NSTimeZone systemTimeZone].secondsFromGMT];
Note that since you are not parsing time, time zone differences shouldn't affect you.
Steps to reproduce
Set the time zone on your machine to Jordan time zone:
NSString *dateString = @"2016-04-01";
NSDateFormatter* parsingFormatter = [[NSDateFormatter alloc] init];
parsingFormatter.dateFormat = @"yyyy-MM-dd";
NSLog(@"Parsed: %@", [parsingFormatter dateFromString:dateString]); // "(null)"
with time zone fix:
NSString *dateString = @"2016-04-01";
NSDateFormatter* parsingFormatter = [[NSDateFormatter alloc] init];
parsingFormatter.dateFormat = @"yyyy-MM-dd";
parsingFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
NSLog(@"Parsed: %@", [parsingFormatter dateFromString:dateString]); // "2016-04-01 00:00:00 +0000"
Upvotes: 5
Reputation: 1815
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.
Upvotes: 1