Search code examples
iosobjective-cnsdatejsqmessagesviewcontroller

JSQMessage set date attribute from unformatted String


I am receiving the chat data from an API and it returns the date of the message as follow :

    30 minutes ago
    1 hour ago
    2 weeks ago 
    ... etc

But in JSQMessage it takes only NSDate, and i searched and failed to find a dynamic way to convert that to an NSDate without a specific format

JSQMessage* message = [[JSQMessage alloc] initWithSenderId:@"3" senderDisplayName:@"Name" date:@"" text:@"msg"];

Any help will be much appreciated.


Solution

  • I did it manually this way since i have no control over the API

    - (int) getCountFromDisplayDate : (NSString*) date {
        NSString* number = @"";
        for (int i = 0;i < date.length;i ++)
            if ([date characterAtIndex:i] == ' ')
                break;
            else
                number = [NSString stringWithFormat:@"%@%c",number,[date characterAtIndex:i]];
        if ([number intValue])
            return [number intValue];
    
        return 0;
    }
    
    - (NSDate*) dateForIndexPath:(NSIndexPath*) indexPath {
        NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat: @"MM/dd/yyyy"];
        NSString* displayDate= _chat[indexPath.item][@"DisplayDate"];
    
        NSString* whatWeDisplay = @"";
        if ([displayDate containsString:@"ago"]) {
            int count = [self getCountFromDisplayDate : displayDate] * -1;
            if ([displayDate containsString:@"second"]) {
                whatWeDisplay = [dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSinceNow:count]];
            }
            else if ([displayDate containsString:@"minute"]) {
                whatWeDisplay = [dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSinceNow:count * 60]];
            }
            else if ([displayDate containsString:@"hour"]) {
                whatWeDisplay = [dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSinceNow:count * 60 * 60]];
            }
            else if ([displayDate containsString:@"day"]) {
                whatWeDisplay = [dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSinceNow:count * 24 * 60 * 60]];
            }
            else if ([displayDate containsString:@"week"]) {
                whatWeDisplay = [dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSinceNow:count * 7 * 24 * 60 * 60]];
            }
            else if ([displayDate containsString:@"month"]) {
                whatWeDisplay = [dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSinceNow:count * 30 * 24 * 60 * 60]];
            }
            else if ([displayDate containsString:@"year"]) {
                whatWeDisplay = [dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSinceNow:count * 365 * 30 * 24 * 60 * 60]];
            }
    
        }
        else
            whatWeDisplay = displayDate;
        NSDate* theDate = [dateFormatter dateFromString:whatWeDisplay];
        if (!theDate) {
    
            return [NSDate date];
        }
        return theDate;
    }