NSDate 无法正确加载/显示
我不确定这是怎么回事。我正在尝试从文件加载 NSString* 对象,使用日期格式化程序将其转换为 NSDate*,然后将小时和分钟组件转换回 NSString,以便我可以在 Interface Builder 中显示时间。然而,我最终得到的不是保存到文件中的时间,而是 19 表示小时,0 表示分钟。 (无论放入什么,程序都会加载四个不同的 NSDate)
这是从文件加载日期的代码(我检查了断点,数组确实有正确的数据,所以这不是问题)
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd hh:mm:ss a"];
date1 = [[df dateFromString:[loadArray objectAtIndex:3]] retain];
这是代码用于显示日期。
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comp = [[gregorian components:NSHourCalendarUnit|NSMinuteCalendarUnit fromDate:myDrug.date1] retain];
hourField1.text = [NSString stringWithFormat:@"%d", comp.hour];
minuteField1.text = [NSString stringWithFormat:@"%d", comp.minute];
(顺便说一句,hourField1 和 分钟Field1 是接收值的 IBOutlet)
我不确定我在哪里出错了,任何帮助将不胜感激。谢谢!
更新: 根据这里一些人的建议,我已经 NSLogged 问题,并且发现日期格式化程序不起作用。示例日期是 2011-02-14 06:00:00 GMT,日期格式化程序是 yyyy-MM-dd hh:mm:ss a,所以我不确定为什么它不起作用。
I'm not sure what's going on with this. I'm trying to load an NSString* object from a file, convert it to an NSDate* with a date formatter, and then convert the hour and minute components back to NSString so I can display a time in Interface Builder. However, instead of the time that was saved to the file, instead I end up with 19 for the hour, and 0 for the minute. (Regardless of what was put in, the program loads four different NSDates)
Here's the code for loading the date from the file (I checked with breakpoints, and the array does indeed have the correct data, so that's not the problem)
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd hh:mm:ss a"];
date1 = [[df dateFromString:[loadArray objectAtIndex:3]] retain];
Here's the code for displaying the date.
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comp = [[gregorian components:NSHourCalendarUnit|NSMinuteCalendarUnit fromDate:myDrug.date1] retain];
hourField1.text = [NSString stringWithFormat:@"%d", comp.hour];
minuteField1.text = [NSString stringWithFormat:@"%d", comp.minute];
(hourField1 and minuteField1 are the IBOutlets that receive the values, by the way)
I'm not sure where I've gone wrong here, and any help would be greatly appreciated. Thanks!
Update:
On the suggestion of some of the people here, I've NSLogged the problem, and I've found that it the date formatter that's not working. An example date is 2011-02-14 06:00:00 GMT, and the date formatter is yyyy-MM-dd hh:mm:ss a, so I'm not sure why it won't work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果 loadArray 中的日期字符串的格式为
2011-02-14 06:00:00 GMT
,则格式应设置如下:我还更改了
hh
到HH
假设小时实际上是 24 小时而不是 12 小时格式。有关详细信息,请参阅 Unicode 日期格式模式。接下来,在显示日期时,如果您想显示 GMT 的小时和分钟,而不是用户当前的时区,则需要设置日历的时区:
If the date strings in loadArray are of the form
2011-02-14 06:00:00 GMT
, then the format should be set as follows:I also changed the
hh
toHH
assuming that the hours are actually in 24-hour instead of 12-hour format. See Unicode Date Format Patterns for details.Next, when displaying the date, if you want to show the hours and minutes in GMT instead of whatever the user's current time zone is, you'll need to set the calendar's time zone: