NSDate 无法正确加载/显示

发布于 2024-10-17 12:20:39 字数 1036 浏览 1 评论 0原文

我不确定这是怎么回事。我正在尝试从文件加载 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼泪也成诗 2024-10-24 12:20:39

如果 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

我还更改了 hhHH 假设小时实际上是 24 小时而不是 12 小时格式。有关详细信息,请参阅 Unicode 日期格式模式

接下来,在显示日期时,如果您想显示 GMT 的小时和分钟,而不是用户当前的时区,则需要设置日历的时区:

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

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:

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. 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:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文