目前我有一个 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
最佳答案
尝试使用此代码,考虑本地时区....
NSDateFormatter *dateFormatterForCurrentDate = [[NSDateFormatter alloc] init];
[dateFormatterForCurrentDate setDateFormat:@"yyyy-MM-dd"];
[dateFormatterForCurrentDate setTimeZone:[NSTimeZone localTimeZone]];
NSDate *myCurrentDate = [dateFormatterForCurrentDate dateFromString:self.currentDate];
希望这对您有所帮助...
关于ios - 将 NSString 转换为 NSDate 会生成错误的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15700607/