如何转换01 Jan 2015 20:33:09到01-02-2015 20:33:09 .it返回nil

时间:2015-02-20 11:22:04

标签: ios iphone nsdateformatter

我试过这个:

NSString *myDateString = @"29 Feb 2016 20:33:09";

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setDateFormat:@"dd MMM yyyy"];

NSDate *myDate = [dateFormatter dateFromString:myDateString];

NSLog(@"%@", myDate);

5 个答案:

答案 0 :(得分:1)

NSString *myDateString = @"29 Feb 2016 20:33:09";
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd MM yyyy HH:mm:ss"];
NSDate* myDate = [dateFormatter dateFromString:myDateString];
NSLog(@"%@", myDate);

答案 1 :(得分:0)

对于您需要的格式,请使用: -

[dateFormatter setDateFormat:@"dd-MM-yyyy HH:mm:ss"];

答案 2 :(得分:0)

您的转换日期nil,因为您没有使用正确的dateFormat 试试:

[dateFormatter setDateFormat:@"dd MMM yyyy HH:mm:ss"];

更多infomation

答案 3 :(得分:0)

NSString * myDateString = @" 2016年2月29日20:33:09&#34 ;;

NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"dd MMM yyyy HH:mm:ss";
NSDate *date = [dateFormatter dateFromString:myDateString];

dateFormatter.dateFormat = @"dd-MM-yyyy HH:mm:ss";
NSString *myDate = [dateFormatter stringFromDate:date];
NSLog(@"%@",myDate);

答案 4 :(得分:0)

NSString * myDateString = @" 2016年2月29日20:33:09&#34 ;;

NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setDateFormat:@" dd MMM yyyy HH:mm:ss"];

NSDate * myDate = [dateFormatter dateFromString:myDateString];

//这里我得到了正确的约会但是

NSLog(@"%@",myDate);

NSString * ss = [NSString stringWithFormat:@"%@",myDate];

NSArray * components = [ss componentsSeparatedByString:@" +"];

NSString * StartnewDateString = [components objectAtIndex:0];

//这里改变了StartnewDateString

的时间