UIDatePicker和NSDateFormatter返回错误的日期

Lev*_*rti 0 iphone objective-c uidatepicker nsdateformatter ios4

我已经尝试过几个帖子,但没有运气.问题是,我面临的可能是白色时区.设备和模拟器UIDatePicker和NSDateFormatter都返回错误的日期,从所选日期(根据设备或mac上设置的当前时区)添加或减去GMT 0的差异.

我已经尝试过设置区域设置和时区但没有任何效果.任何想法都很棒!谢谢.

(尚未发布任何代码示例,因为两者都已初始化而未立即设置任何属性.)

更新1:

这是代码片段:

 datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0.0, frame.size.height - 216.0, frame.size.width, 216.0)];

    [self addSubview:datePicker];

    ....


    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

    [formatter setDateFormat:@"EEE, MMM d, y h:mma"];

    NSString *dateString = [formatter stringFromDate:datePicker.date];
    [formatter release];
Run Code Online (Sandbox Code Playgroud)

Lev*_*rti 14

找到解决方案,应按以下方式添加时区差异.

- (NSDate *)dateToGMT:(NSDate *)sourceDate {
    NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
    NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
    NSDate* destinationDate = [[[NSDate alloc] initWithTimeInterval:destinationGMTOffset sinceDate:sourceDate] autorelease];
    return destinationDate;
}
Run Code Online (Sandbox Code Playgroud)