ios - 将 NSString 转换为 NSDate 会生成错误的值

标签 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

最佳答案

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

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/

相关文章:

objective-c - iOS 5 - Rails 3 服务器 - 共享 key

ios - 时间戳不会改变字符串格式

ios - iOS 13 中的 GMSPlacePickerViewController 问题

ios - Xcode UI 测试 - 通过 id 在 Webview 中查找元素

iphone - 从iPhone以编程方式将视频流式传输到服务器

objective-c - Cocoa中的CMD+Option+D模拟

ios - 使用其他iOS App登录(类似于使用Facebook登录)

ios - 从 iOS 上传图片到 PHP 服务器

带有 URL 检测的 iPhone 的 UILabel

Objective-C = 运算符 vs stringWithString/arrayWithArray