Date does not appear in UILabel

前端 未结 2 1613
忘了有多久
忘了有多久 2021-01-24 13:00

What is wrong with this code?

    NSString *strTest = @\"Sun Apr 12 23:29:24 +0000 2009\";
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
          


        
2条回答
  •  春和景丽
    2021-01-24 13:28

    Your date formatter date format does not match the date format from the strTest string.

    NSString *strTest = @"Sun Apr 12 23:29:24 +0000 2009";
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    NSLocale *loc = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]; // so the formatter recognizes english names for months and days
    [dateFormatter setLocale:loc];
    [loc release]; // remove if using ARC
    [dateFormatter setDateFormat:@"EEE MMM d HH:mm:ss ZZZZ yyyy"];
    NSDate *createdAtFormatted = [dateFormatter dateFromString:strTest];
    NSLog(@"got %@", createdAtFormatted);
    

    The formatter specifiers can be found here.

提交回复
热议问题