如何从字符串中获取日期?我的代码附在此处

时间:2013-12-12 11:34:20

标签: ios nsdateformatter

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];

NSDate *date1 =[[NSDate alloc] init];
date1 = [dateFormat dateFromString:@"11-nov-2013"];
NSLog(@"date is %@",date1); // it returns null
self.datePicker.date = date1;
[dateFormat release];

3 个答案:

答案 0 :(得分:1)

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd-MMM-yyyy"];
NSDate *myDate = [formatter dateFromString:@"11-nov-2013"];
NSLog(@"date is %@",myDate);

答案 1 :(得分:0)

将日期格式设为

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

答案 2 :(得分:0)

您还需要设置日期格式:

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