Objective-C returns 中的日期格式化程序 null
Date formatter in Objective-C returns null
我不明白为什么我的 dateFormatter
returns 为空。代码非常简单,但我没有看到这里有任何错误。我也尝试过使用 setDateFormat
,但没有任何结果。代码有什么问题?
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
mikey.hireDate = [dateFormatter dateFromString:@"Aug 02, 2010"];
将日期格式化程序的区域设置设置为 EN_US_POSIX
。这样,设备的区域就不会改变格式化程序。
NSLocale *locale = [[NSLocale alloc]
initWithLocaleIdentifier:@"EN_US_POSIX"];
[dateFormatter setLocale:locale];
你的代码可以在我的机器上运行;话虽如此,如果您想从用户输入中解析日期字符串,NSDataDetector 通常是比 NSDateFormatter 更好的方法。
我不明白为什么我的 dateFormatter
returns 为空。代码非常简单,但我没有看到这里有任何错误。我也尝试过使用 setDateFormat
,但没有任何结果。代码有什么问题?
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
mikey.hireDate = [dateFormatter dateFromString:@"Aug 02, 2010"];
将日期格式化程序的区域设置设置为 EN_US_POSIX
。这样,设备的区域就不会改变格式化程序。
NSLocale *locale = [[NSLocale alloc]
initWithLocaleIdentifier:@"EN_US_POSIX"];
[dateFormatter setLocale:locale];
你的代码可以在我的机器上运行;话虽如此,如果您想从用户输入中解析日期字符串,NSDataDetector 通常是比 NSDateFormatter 更好的方法。