尝试在Objective-C中解析日期时出错 [英] Bug when trying to parse date in Objective-C

查看:111
本文介绍了尝试在Objective-C中解析日期时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我试图将 4月从04(MM)转换为 4月(MMM)时,它又转回 nil ,这是iOS中的已知问题还是我做错了什么?



这是我的检查代码:

  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];
[格式化程序setDateFormat:@MMM];
NSString * stringFromDate = [formatter stringFromDate:myDate];
NSLog(@%@ ==%@,dateString,stringFromDate);

更改 dataString = @2015-04-01 stringFromDate 返回 4月



这是我的输出:



  NSString * dateString = @2016-04-01 ; 
NSDateFormatter * parsingFormatter = [[NSDateFormatter alloc] init];
parsingFormatter.dateFormat = @yyyy-MM-dd;

NSLog(@Parsed:%@,[parsingFormatter dateFromString:dateString]); //(null)

带有时区修复:

  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


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

解决方案

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"

这篇关于尝试在Objective-C中解析日期时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆