NSComponents components:fromDate:toDate:options:测量小时或天吗?



编辑:请标记为副本,答案在这里

所以我想看看用户上次发布对象的日期是否已经过去了。如果我使用

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

例子:用户在晚上9点发布对象。然后我在第二天早上8点(11个小时后)运行上面的方法。它会告诉我0天过去了,对吧?如果是这样,是否有一种方法可以告诉我我想要找出什么,或者我应该手动去做?

试试这个方法,希望对你有帮助

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

最新更新