我有NSDateFormatter的问题 我在一个视图中从选择器转换日期
`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];
` 然后将字符串发送到其他笔尖
用该代码将其转换回来
`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);`
它返回错误的日期 发送2011-09-26 01:02:49 geting 2011-09-25 22:02:49 +0000
出了什么问题?
答案 0 :(得分:5)
由于:
NSLog()
NSDate
时,总是将其记录在GMT中答案 1 :(得分:0)
在您的日期格式字符串中包含时区。
答案 2 :(得分:0)
+ (NSDate*) dateFromString:(NSString*)aStr
{
if (aStr.length == 10) {
aStr = [aStr stringByAppendingFormat:@" 00:00:00"];
}
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MM-dd-yyyy HH:mm:ss"];
dateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
NSLog(@"strdate %@", aStr);
NSDate *aDate = [dateFormatter dateFromString:aStr];
NSLog(@"date = %@",aDate);
[dateFormatter release];
return aDate;
}