-[NSDateFormatter dateFromString:] 返回 nil
NSString *lower = [NSString stringWithFormat:@"%@",[newDates objectAtIndex:0]];
NSString *higher = [NSString stringWithFormat:@"%@",[newDates objectAtIndex:[newDates count]-1]];
NSLog(@"%@",lower);
NSLog(@"%@",higher);
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd"];
NSDate *dtLower = [df dateFromString:lower];
NSDate *dtHigher = [df dateFromString:higher];
[df release];
NSDateFormatter *df1 = [[NSDateFormatter alloc] init];
[df1 setDateFormat:@"yyyy-MM-dd"];
NSString * lowerDate = [df1 stringFromDate:dtLower];
NSString * higherDate = [df1 stringFromDate:dtHigher];
UILabel *dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 31, self.view.frame.size.width, 30)];
[dateLabel setTextColor:[UIColor grayColor]];
dateLabel.text = [NSString stringWithFormat:@"%@ - %@",lowerDate,higherDate];
[self.view addSubview:dateLabel];
控制台中 lower
和 higher
的输出是:
2011-05-23 14:55:52.767 Vocab[4225:207] 2011-05-23 03:58:22
2011-05-23 14:55:53.781 Vocab[4225:207] 2011-05-23 07:14:56
这里上面的代码对我不起作用。 higher
和 lower
的值不为空,但是当我尝试使用 NSDateFormatter 将其转换为 NSDate 时,它返回 nil。
所以 dtLower
和 dtHigher
返回 nil
可能出了什么问题?
NSString *lower = [NSString stringWithFormat:@"%@",[newDates objectAtIndex:0]];
NSString *higher = [NSString stringWithFormat:@"%@",[newDates objectAtIndex:[newDates count]-1]];
NSLog(@"%@",lower);
NSLog(@"%@",higher);
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd"];
NSDate *dtLower = [df dateFromString:lower];
NSDate *dtHigher = [df dateFromString:higher];
[df release];
NSDateFormatter *df1 = [[NSDateFormatter alloc] init];
[df1 setDateFormat:@"yyyy-MM-dd"];
NSString * lowerDate = [df1 stringFromDate:dtLower];
NSString * higherDate = [df1 stringFromDate:dtHigher];
UILabel *dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 31, self.view.frame.size.width, 30)];
[dateLabel setTextColor:[UIColor grayColor]];
dateLabel.text = [NSString stringWithFormat:@"%@ - %@",lowerDate,higherDate];
[self.view addSubview:dateLabel];
Output of lower
and higher
in Console is:
2011-05-23 14:55:52.767 Vocab[4225:207] 2011-05-23 03:58:22
2011-05-23 14:55:53.781 Vocab[4225:207] 2011-05-23 07:14:56
Here the above code does not work for me. The value for higher
and lower
are not null but when I try converting it into NSDate using NSDateFormatter then it returns nil.
So dtLower
and dtHigher
return nil
What could be wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试设置
[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
Try setting
[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
您的第一个日期格式错误。
Your first date format is wrong.
如果您使用用户可见的日期,则应避免设置日期格式字符串,因为很难预测格式字符串在所有可能的用户配置中的表达方式。相反,您应该尝试限制自己设置日期和时间样式(通过
-[NSDateFormatter setDateStyle:]
和-[NSDateFormatter setTimeStyle:])
。如果您使用固定格式日期,则应首先将日期格式化程序的区域设置设置为适合您的固定格式的区域设置。在大多数情况下,最好选择的区域设置是
en_US_POSIX
,该区域设置是专门为生成美国英语结果而设计的,无论用户和系统首选项如何。en_US_POSIX
在时间和机器之间也是不变的(en_US_POSIX
在 iOS 上的工作方式与在 OSX 上的工作方式相同,以及在其他平台上的工作方式相同)。将
en_US_POSIX
设置为日期格式化程序的区域设置后,您就可以设置日期格式字符串,并且日期格式化程序将对所有用户保持一致的行为。上述信息及更多信息可以在Apple的技术问答QA1480<中找到/a>
这是实现上述技术说明中建议的代码片段:
If you're working with user-visible dates, you should avoid setting a date format string as it's very hard to predict how your format string will be expressed in all possible user configurations. Rather, you should try and limit yourself to setting date and time styles (via
-[NSDateFormatter setDateStyle:]
and-[NSDateFormatter setTimeStyle:])
.If you're working with fixed-format dates, you should first set the locale of the date formatter to something appropriate for your fixed format. In most cases the best locale to choose is
en_US_POSIX
, a locale that's specifically designed to yield US English results regardless of both user and system preferences.en_US_POSIX
is also invariant in time and between machines (en_US_POSIX
works the same on iOS as it does on OSX, and as it it does on other platforms).Once you've set
en_US_POSIX
as the locale of the date formatter, you can then set the date format string and the date formatter will behave consistently for all users.The above info and more can be found in Apple's Technical Q&A QA1480
Here's a snippet of code which implements the recommendation from the above Technical Note: