我正在使用以下代码转换NSString
为NSDate
:
NSDate *date = [NSDate convertStringToDate:strDate andFormatter:@"MM dd"];
+ (NSDate *)convertStringToDate:(NSString *)dateString andFormatter:(NSString *)formatter
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// this is imporant - we set our input date format to match our input string
// if format doesn't match you'll get nil from your string, so be careful
[dateFormatter setDateFormat:formatter];
dateFormatter.timeZone = [NSTimeZone systemTimeZone];
return [dateFormatter dateFromString:dateString];
}
NSString 是@"07 15"
. 但我的 NSDate 是nil
. 在模拟器上它很好,但在 iOS7 设备上它是零。
你有什么建议吗?