Date does not appear in UILabel

前端 未结 2 1611
忘了有多久
忘了有多久 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.

    0 讨论(0)
  • 2021-01-24 13:34

    You are giving MM/dd/yy pattern to dateFormatter but your date string is not fit for that thus dateFormatter returning a nil date.

    0 讨论(0)
提交回复
热议问题