简体   繁体   English

NSDate无法正确加载/显示

[英]NSDate won't load/display correctly

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. 我试图从文件加载NSString *对象,使用日期格式将其转换为NSDate *,然后将小时和分钟组件转换回NSString,以便在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. 但是,不是将时间保存到文件中,而是将小时数设为19,将分钟数设为0。 (Regardless of what was put in, the program loads four different NSDates) (无论放入什么内容,程序都会加载四个不同的NSDate)

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) (顺便说一下,hourField1和minuteField1是接收值的IBOutlet)

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. 更新:在这里的一些人的建议下,我已经NSLogged问题,并且我发现它的日期格式化程序不起作用。 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. 示例日期为2011-02-14 06:00:00 GMT,日期格式为yyyy-MM-dd hh:mm:ss a,因此我不确定为什么它不起作用。

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: 如果loadArray中的日期字符串的格式为2011-02-14 06:00:00 GMT ,则格式应设置如下:

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd HH:mm:ss zzz"];                     <--
date1 = [[df dateFromString:[loadArray objectAtIndex:3]] retain];
    // the retain above is suspicious btw (but that's another question)
[df release];  //don't forget this

I also changed the hh to HH assuming that the hours are actually in 24-hour instead of 12-hour format. 我也将hh更改为HH假设小时数实际上是24小时而不是12小时格式。 See Unicode Date Format Patterns for details. 有关详细信息,请参见Unicode日期格式模式

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: 接下来,在显示日期时,如果要以格林尼治标准时间显示小时和分钟,而不是用户当前的时区,则需要设置日历的时区:

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSTimeZone *tz = [NSTimeZone timeZoneWithName:@"GMT"];             <--
[gregorian setTimeZone:tz];                                        <--
NSDateComponents *comp = [gregorian components:NSHourCalendarUnit|NSMinuteCalendarUnit fromDate:myDrug.date1];
  //do not do a retain on comp above
hourField1.text = [NSString stringWithFormat:@"%d", comp.hour];
minuteField1.text = [NSString stringWithFormat:@"%d", comp.minute];
[gregorian release];  //don't forget this

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM