0

EDIT: PLEASE MARK AS DUPLICATE, ANSWER FOUND HERE

So I want to see if the day has passed since the user last posted an object. If I use

NSCalendarUnit units = NSDayCalendarUnit;
NSDateComponents *components = [calendar components:units fromDate:lastPostDate toDate:now options:0];

Example: The user posts the object at 9pm. Then I run the method above at 8am the next day (11 hours later). It would tell me that 0 days has passed right? If so, is there a method that tells me what I am trying to find out or should I go at it manually?

Community
  • 1
  • 1
apolo
  • 441
  • 4
  • 18
  • I did and got what I thought I would get. I posted the question as is because I wanted to leave it open in the case that there was an option/way to do what I want. Also, my newbie confidence in this makes me doubt myself. – apolo May 12 '14 at 05:02

1 Answers1

-1

Try this method i hope its helpfull

- (NSString*)getTimeDifferenceFromCurrentDate:(NSDate *) lastPostDate {
    NSDate *currentDate = [NSDate date];

    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit|NSWeekCalendarUnit fromDate:messageDate toDate:currentDate options:0];


    NSString *strTime = @"";

    /*if (components.day > 0 || components.week >= 1) {
     if (components.day >= 1||components.week >= 1||components.month >= 1){
     NSDateFormatter *dfForCell = [[NSDateFormatter alloc] init];

     [dfForCell setDateFormat:@"MMM dd, yyyy"];
     NSLocale*  my24HourLocale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease];
     [dfForCell setLocale:my24HourLocale];
     NSString *aStrFinalDate  = [dfForCell stringFromDate:messageDate];
     strTime = [NSString stringWithFormat:@"%@", aStrFinalDate];
     };
     }*/
    if (components.year > 0) {
        if (components.year > 1)
            strTime = [NSString stringWithFormat:@"%d years ago", components.year];
        else
            strTime = [NSString stringWithFormat:@"%d year ago", components.year];
    }
    else if (components.month > 0) {
        if (components.month > 1)
            strTime = [NSString stringWithFormat:@"%d months ago", components.month];
        else
            strTime = [NSString stringWithFormat:@"%d month ago", components.month];
    }
    else if (components.week > 0) {
        if (components.week > 1)
            strTime = [NSString stringWithFormat:@"%d weeks ago", components.day];
        else
            strTime = [NSString stringWithFormat:@"%d day ago", components.day];
    }
    else if (components.day > 0) {
        if (components.day > 1)
            strTime = [NSString stringWithFormat:@"%d days ago", components.day];
        else
            strTime = [NSString stringWithFormat:@"%d day ago", components.day];
    }
    else if (components.hour > 0) {
        if (components.hour > 1)
            strTime = [NSString stringWithFormat:@"%dhours ago", components.hour];
        else
            strTime = [NSString stringWithFormat:@"%dhour ago", components.hour];
    }
    else if (components.minute > 0) {
        if (components.minute > 1)
            strTime = [NSString stringWithFormat:@"%dmin ago", components.minute];
        else
            strTime = [NSString stringWithFormat:@"%dmin ago", components.minute];
    }
    else if (components.second > 0) {
        if (components.second > 1)
            strTime = [NSString stringWithFormat:@"%dsec ago", components.second];
        else
            strTime = [NSString stringWithFormat:@"%dsec ago", components.second];
    }
    else {
        strTime = @"1 sec ago";
    }
    return strTime;
}
Bipin Patel
  • 228
  • 1
  • 5
  • 2
    Instead of just showing a bunch of code, it would be more helpful if you explain how this answers the posted question. – Martin R May 12 '14 at 05:23