NSDate到NSString返回nil

时间:2013-09-04 12:26:25

标签: nsstring nsdate nsdateformatter

 NSString *dateString = @"13-8-06 10:53:54 +0300";
    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:@"yy-MM-dd HH:mm 'ZZ'"];
    NSDate *dateFromString = [[NSDate alloc] init];
    // voila!
    dateFromString = [dateFormatter dateFromString:dateString];

dateFromString为零。有谁知道我应该给NSDateFormatter哪种格式?

4 个答案:

答案 0 :(得分:0)

试试这个

NSString *dateString = @"13-8-06 10:53:54 +0300";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yy HH:mm:ss ZZZ"];
NSDate *date =[dateFormatter dateFromString:dateString];
NSLog(@"%@",date);
[dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss ZZZ"];
dateString =[dateFormatter stringFromDate:date];
NSLog(@"%@",dateString);

答案 1 :(得分:0)

您必须将日期格式设置为

@"dd-MM-yy HH:mm:ss ZZZ"

答案 2 :(得分:0)

    NSString *dateString = @"13-8-06 10:53:54 +0300";
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yy-M-dd HH:mm:ss Z"];
    NSDate *date = [dateFormatter dateFromString:dateString];
    NSLog(@"%@",date);

答案 3 :(得分:0)

试试这个[dateFormatter setDateFormat:@"yy-MM-dd HH:mm:ss ZZ"];