开发者

NSDate won't load/display correctly

开发者 https://www.devze.com 2023-02-10 05:40 出处:网络
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 N

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|NSMinut开发者_如何学运维eCalendarUnit 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.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消