从字符串返回的字符串错误的日期!为什么?

1 iphone objective-c ipad ios

我在NSDateFormetter中遇到问题我在一个视图中从选择器转换日期

`NSDateFormatter *output = [[NSDateFormatter alloc] init];
[output setDateStyle:NSDateFormatterMediumStyle];
[output setDateStyle:NSDateFormatterShortStyle];
[output setDateFormat:@"yyyy-MM-dd hh:mm:ss"];    
NSString *StringToSend = [output stringFromDate:datePicker.date];
Run Code Online (Sandbox Code Playgroud)

`然后将字符串发送到其他笔尖

用该代码将其转换回来的地方

`NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setDateStyle:NSDateFormatterMediumStyle];
[inputFormatter setDateStyle:NSDateFormatterShortStyle];
[inputFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
NSDate *formatterDate = [inputFormatter dateFromString:StringFromOtherView];
NSLog(@"%@", formatterDate);`
Run Code Online (Sandbox Code Playgroud)

并且它返回错误的日期发送2011-09-26 01:02:49 geting 2011-09-25 22:02:49 +0000

怎么了?

Dav*_*ong 5

因为:

  1. 当你NSLog()NSDate,它始终会记录它GMT
  2. 你住的时间比格林威治标准时间早3个小时.因此,上午1:02对你来说是格林尼治标准时间下午22:02(前一天).