使用keyValue在uilabel中进行Json日期转换

时间:2015-12-28 10:20:26

标签: ios json nsdateformatter

您好,在我的项目中,我在json中获取了日期格式,我尝试了很多格式。但没有成功。 ExpiryDate 是我的keyvalue存储在dictionary.please让我知道这个转换。我需要这样的格式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";

3 个答案:

答案 0 :(得分:0)

试试这个

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

答案 1 :(得分:0)

您从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];

答案 2 :(得分:0)

您好我找到了答案。谢谢您的回复

 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;