Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert UNIX timestamp into Data - iOS

I have an iOS application which parses a JSON feed for data. In this data are some UNIX timestamps which I am storing in a NSString. What I want to do is to convert these timestamps into dates (month and day). But I am trying to do this without doing any division myself, because from what I have read online, you should always use Apple's API's to do the conversion for an accurate result.

However my code is not working and I get this error:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber length]: unrecognized selector sent to instance 

Here is my code:

NSString *time_string = [timestamps objectAtIndex:indexPath.row];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd"];
NSDate *date = [formatter dateFromString:time_string];

cell.dateOfPub.text = [NSString stringWithFormat:@"%@", date];

I don't understand what I am doing wrong. Any help would be greatly appreciated.

Thanks, Dan

like image 591
Supertecnoboff Avatar asked Jan 31 '26 00:01

Supertecnoboff


1 Answers

double unixTimeStamp = [time_string doubleValue];
NSTimeInterval _interval=unixTimeStamp;
NSDate *date = [NSDate dateWithTimeIntervalSince1970:_interval];
NSDateFormatter *_formatter=[[NSDateFormatter alloc]init];
[_formatter setLocale:[NSLocale currentLocale]];
[_formatter setDateFormat:@"dd.MM.yyyy"];
NSString *_date=[_formatter stringFromDate:date];
like image 200
Magyar Miklós Avatar answered Feb 02 '26 15:02

Magyar Miklós



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!