Json uilabel 中带键值的日期转换

Json date conversion in uilabel with keyValue

你好,在我的项目中,我在 json 中得到了日期格式,我尝试了很多 formats.it 都没有成功。ExpiryDate 是我存储在 dictionary.please 让我知道 this.I 的转换需要这样的格式 07/Jan/2016.

 cell.Expire.text=[ResDiction objectForKey:@"ExpiryDate"];

 NSString *dateString =  [ResDiction objectForKey:@"ExpiryDate"];
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
        NSDate *date = [dateFormatter dateFromString:dateString];

我的 json 结果:

        CreateUserId = 2;
        ExpiryDate = "2016-01-07T00:00:00";

试试这个

cell.Expire.text=[NSString stringWithFormat :"%@",[ResDiction objectForKey:@"ExpiryDate"]];

您从 sting 到 date 的转换似乎是正确的。

但是,您需要使用另一个 NSDateFormatter 才能以其他格式输出日期。

NSString *dateString =  [ResDiction objectForKey:@"ExpiryDate"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"]; //Will convert from string to native date object
NSDate *date = [dateFormatter dateFromString:dateString];

NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
[dateFormatter2 setDateFormat:@"dd/MMM/yyyy"]; //will convert into string of type 07/Jan/2016
cell.Expire.text = [dateFormatter2 stringFromDate:date];

您好,我只是为您的所有回复找到了 answer.Thanks

 NSString * dateStr = [ResDiction objectForKey:@"ExpiryDate"];
    NSArray *dateStrParts = [dateStr componentsSeparatedByString:@"T"];
    NSString *datePart = [dateStrParts objectAtIndex:0];
    NSString *newDateStr = [NSString stringWithFormat:@"%@",datePart];

    NSDateFormatter *df = [[NSDateFormatter alloc] init];
   [df setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
   [df setDateFormat:@"yyyy-MM-dd"];
    NSLog(@"%@",newDateStr);
    cell.Expire.text=newDateStr;