将NSString转换为NSDate会产生错误的值

时间:2013-03-29 09:30:37

标签: ios objective-c

目前我有一个NSString值,其中包含以下日期作为当前日期

(lldb) po self.currentDate
(NSString *) $5 = 0x002ca8d0 2013-03-29

但是当我将这个NSString转换为NSDate时,它显示错误的值

NSDate *myCurrentDate  = [dateFormatterForCurrentDate dateFromString:self.currentDate];

(lldb) po myCurrentDate
(NSDate *) $4 = 0x002cf030 2013-03-28 18:30:00 +0000

2 个答案:

答案 0 :(得分:3)

尝试使用此代码,以考虑当地时区....

NSDateFormatter *dateFormatterForCurrentDate = [[NSDateFormatter alloc] init];
[dateFormatterForCurrentDate setDateFormat:@"yyyy-MM-dd"];
[dateFormatterForCurrentDate setTimeZone:[NSTimeZone localTimeZone]];
NSDate *myCurrentDate  = [dateFormatterForCurrentDate dateFromString:self.currentDate];

希望这有用......

答案 1 :(得分:2)

设置此日期格式:

    [dateFormatterForCurrentDate setDateFormat:@"yyyy-MM-dd"];

并检查developer.apple.com中的Date Formatters

可能对你有用。