-1

I get datetime from server in double and when I convert it to date string in ios, I get error of EXEC_BAD_ACCESS from the first line of code below.

For example,

date string from server: 1426923003

This is how I convert it:

     NSString *dateFromServer = [NSString stringWithFormat:@"%@" ,news.dateCreated];
    NSLog(@"date server: %@",dateFromServer);
    long long theDate = [dateFromServer longLongValue];

    NSTimeInterval interval=theDate;

    NSDate *datenew = [NSDate dateWithTimeIntervalSince1970:interval];
    NSDateFormatter *_formatter=[[NSDateFormatter alloc]init];
    [_formatter setLocale:[NSLocale currentLocale]];
    [_formatter setDateFormat:@"dd-MM-yyyy hh:mm a"];
    dateString=[_formatter stringFromDate:datenew];

I run this codes in method: -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{} in order to show date on each cell of tableview.

I don't know why it has error like this. How do we make work ? Thank you.

malinchhan
  • 767
  • 2
  • 8
  • 28

1 Answers1

0

Date from server contains 13 digits..assign the date to long long instead of double

Sumeet
  • 1,055
  • 8
  • 18
  • Did u try `long long`?? – Sumeet Mar 21 '15 at 08:49
  • server side gives me all in json and this date as string, I don't know how they convert it – malinchhan Mar 21 '15 at 08:53
  • when I use long long, date number is 2022824320 – malinchhan Mar 21 '15 at 08:55
  • and by my codes above, it gives result: 06-02-2034 02:38 PM – malinchhan Mar 21 '15 at 08:58
  • I am wrong when I convert date from server , correct timestamp I recieve is 1426923003 that I can convert directly. – malinchhan Mar 21 '15 at 09:25
  • When u get from server ul get nsstring 1426923003 right? – Sumeet Mar 21 '15 at 11:27
  • Then use longlongvalue method of nsstring to extract it to a long long.. Like this – Sumeet Mar 21 '15 at 11:28
  • Say the string is `serverdate = @"1426923003`… then do the following `long long theDate = [server longlongvalue];` – Sumeet Mar 21 '15 at 11:31
  • Say the string is `serverdate = @"1426923003`… then do the following `long long theDate = [serverdate longlongvalue];` – Sumeet Mar 21 '15 at 11:34
  • when I try it, I get signal SIGABRT. I use this code: long long theDate = [news.dateCreated longLongValue]; NSTimeInterval interval=theDate; NSDate *datenew = [NSDate dateWithTimeIntervalSince1970:interval]; NSDateFormatter *_formatter=[[NSDateFormatter alloc]init]; [_formatter setLocale:[NSLocale currentLocale]]; [_formatter setTimeZone:[NSTimeZone localTimeZone]]; [_formatter setDateFormat:@"dd-MM-yyyy hh:mm a"]; dateString=[_formatter stringFromDate:datenew]; – malinchhan Mar 21 '15 at 12:31
  • Even I change like this: NSString *dateFromServer = news.dateCreated; NSLog(@"date server: %@",dateFromServer); long long theDate = [dateFromServer longLongValue]; NSTimeInterval interval=theDate; – malinchhan Mar 21 '15 at 12:37
  • Which line do u get d exception – Sumeet Mar 21 '15 at 13:38
  • news.dateCreated is a string right?? What is the type of news – Sumeet Mar 21 '15 at 13:41
  • error this line: NSString *dateFromServer = [NSString stringWithFormat:@"%@" ,news.dateCreated]; – malinchhan Mar 21 '15 at 14:36
  • news.dateCreated is NSString and type of news is NSOBject – malinchhan Mar 21 '15 at 14:37
  • After I assign double to server date and then long long , now it works without error. Thanks u – malinchhan Mar 22 '15 at 05:54