P.VenkataVamsi P.VenkataVamsi - 1 year ago 82
iOS Question

date format is not implementing correctly

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. output is date-joined - 18/01/1970



response is list

( { 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; } ); } )

Answer Source

I have changed your code to get the result:

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);

Output:

date-joined - 24/11/2016

Your issue is in while converting datestr to date i.e divided by 1000. Don't do that. Just change it and you will get your required result

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download