Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
314 views
in Technique[技术] by (71.8m points)

ios - Bug when trying to parse date in Objective-C

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:

this is the log image

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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: enter image description here

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"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...