日期格式未正确实施

时间:2016-11-24 06:44:55

标签: ios objective-c cocoa-touch nsdate nsdateformatter

self.storeArray = [dataDict valueForKey:@"list"];
for (int i=0; i<self.storeDict.count; i++) {
    NSDictionary * dateDic1=[self.storeArray objectAtIndex:i];
    NSLog(@"dict11 %@",dateDic1);
    NSString* dateStr = [dateDic1 objectForKey:@"dt"];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:([dateStr longLongValue] / 1000)];
    NSDateFormatter *datfrm = [[NSDateFormatter alloc] init];
    [datfrm setDateFormat:@"dd/MM/yyyy"];
    NSString *strDate = [datfrm stringFromDate:date];
    NSLog(@"date-joined - %@",strDate);
    self.dateArry addObject:strDate];
}
  1. 输出日期加入 - 1970年1月18日
  2. 响应是列表

    ( { clouds = 0; 
        deg = 91; 
        dt = 1479967200; 
        humidity = 34; 
        pressure = "960.53"; 
        speed = "1.51"; 
        temp = { day = "35.2"; eve = "27.14"; max = "35.2"; min = "11.39"; morn = "28.91"; night = "11.39"; }; 
        weather = ( { description = "clear sky"; icon = 01d; id = 800; main = Clear; } );},
    
        { clouds = 0; 
          deg = 102; 
          dt = 1480053600; 
          humidity = 33; 
          pressure = "960.16"; 
          speed = "1.46"; 
          temp = { day = "29.01"; eve = "24.46"; max = "30.71"; min = "9.15"; morn = "9.15"; night = "11.83";};
        weather = ( { description = "clear sky"; icon = 01d; id = 800;
        main = Clear; } ); }, 
    
        { clouds = 0; 
          deg = 120; 
          dt = 1480140000;
          humidity = 37; 
          pressure = "960.83"; 
          speed = "1.62"; 
          temp = { day = "28.51"; eve = "24.42"; max = "30.33"; min = "10.66"; morn = "10.66";    night = "11.98"; }; 
          weather = ( { description = "clear sky"; icon = 01d; id = 800; main = Clear; } ); }, 
    
          { clouds = 0; 
            deg = 65; 
            dt = 1480226400; 
            humidity = 0; 
            pressure = "987.92";
            speed = "1.27"; 
            temp = { day = "28.7"; eve = "25.25"; max = "28.7"; min = "12.85"; morn = "12.85"; night = "15.51"; }; 
            weather = ( { description =    "clear sky"; icon = 01d; id = 800; main = Clear; } ); }, 
          { clouds = 0; 
            deg = 104; 
            dt = 1480312800;
            humidity = 0; 
            pressure = "987.88"; 
            speed = "1.39"; 
            temp = { day = "29.56"; eve = "26.25"; max = "29.56"; min = "13.13"; morn = "13.13"; night = "16.16"; }; 
            weather = ( { description = "light rain"; icon = 10d; id = 500; main = Rain; } ); }    )
    

1 个答案:

答案 0 :(得分:2)

我已更改您的代码以获得结果:

NSString* dateStr = @"1479967200";
NSDate *date = [NSDate dateWithTimeIntervalSince1970:([dateStr doubleValue])];
NSDateFormatter *datfrm = [[NSDateFormatter alloc] init];
datfrm.locale = [NSLocale currentLocale];
[datfrm setDateFormat:@"dd/MM/yyyy"];
NSString *strDate = [datfrm stringFromDate:date];
NSLog(@"date-joined - %@",strDate);

输出:

  

约会 - 2016年11月24日

你的问题在于将datetr转换为date,即除以1000.不要这样做。只需更改它,您就可以获得所需的结果