Convert date string into proper format

后端 未结 2 1522
抹茶落季
抹茶落季 2021-01-29 15:41

I am getting a response from a server and there is a date string I need to convert into a date:

Thu Jun 29 07:15:25 +0000 2017

I am

2条回答
  •  说谎
    说谎 (楼主)
    2021-01-29 15:58

    This is how you can format it in "Thu Jun 29"

    -(NSString *)formatCreatedAt:(NSString *)createdAt
    {
        NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
        [dateFormat setDateFormat:@"EEE MMM dd HH:mm:ss Z yyyy"];
        NSDate *date = [dateFormat dateFromString:createdAt];
    
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"EEE MMM dd"];
    
        NSString *stringFromDate = [formatter stringFromDate:date];
        return stringFromDate;
    }
    

    Pass the value of created_at in this method and the output will be returned in your specified format.

提交回复
热议问题