How to convert NSDate in to relative format as “Today”,“Yesterday”,“a week ago”,“a month ago”,“a year ago”?

前端 未结 16 2075
天命终不由人
天命终不由人 2020-11-28 22:08

I want to convert nsdate in to relative format like \"Today\",\"Yesterday\",\"a week ago\",\"a month ago\",\"a year ago\",\"date as it is\".

I have writ

16条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-28 22:34

    Here is code I created for my use:

    + (NSString*) getTimestampForDate:(NSDate*)date {
    
        NSDate* sourceDate = date;
    
        // Timezone Offset compensation (optional, if your target users are limited to a single time zone.)
    
        NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithName:@"America/New_York"];
        NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
    
        NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
        NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
    
        NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
    
        NSDate* destinationDate = [[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate];
    
        // Timestamp calculation (based on compensation)
    
        NSCalendar* currentCalendar = [NSCalendar currentCalendar];
        NSCalendarUnit unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit;
    
        NSDateComponents *differenceComponents = [currentCalendar components:unitFlags fromDate:destinationDate toDate:[NSDate date] options:0];//Use `date` instead of `destinationDate` if you are not using Timezone offset correction
    
        NSInteger yearDifference = [differenceComponents year];
        NSInteger monthDifference = [differenceComponents month];
        NSInteger dayDifference = [differenceComponents day];
        NSInteger hourDifference = [differenceComponents hour];
        NSInteger minuteDifference = [differenceComponents minute];
    
        NSString* timestamp;
    
        if (yearDifference == 0
            && monthDifference == 0
            && dayDifference == 0
            && hourDifference == 0
            && minuteDifference <= 2) {
    
            //"Just Now"
    
            timestamp = @"Just Now";
    
        } else if (yearDifference == 0
                   && monthDifference == 0
                   && dayDifference == 0
                   && hourDifference == 0
                   && minuteDifference < 60) {
    
            //"13 minutes ago"
    
            timestamp = [NSString stringWithFormat:@"%ld minutes ago", (long)minuteDifference];
    
        } else if (yearDifference == 0
                   && monthDifference == 0
                   && dayDifference == 0
                   && hourDifference == 1) {
    
            //"1 hour ago" EXACT
    
            timestamp = @"1 hour ago";
    
        } else if (yearDifference == 0
                   && monthDifference == 0
                   && dayDifference == 0
                   && hourDifference < 24) {
    
            timestamp = [NSString stringWithFormat:@"%ld hours ago", (long)hourDifference];
    
        } else {
    
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            [formatter setLocale:[NSLocale currentLocale]];
    
            NSString* strDate, *strDate2 = @"";
    
            if (yearDifference == 0
                && monthDifference == 0
                && dayDifference == 1) {
    
                //"Yesterday at 10:23 AM", "Yesterday at 5:08 PM"
    
                [formatter setDateFormat:@"hh:mm a"];
                strDate = [formatter stringFromDate:date];
    
                timestamp = [NSString stringWithFormat:@"Yesterday at %@", strDate];
    
            } else if (yearDifference == 0
                       && monthDifference == 0
                       && dayDifference < 7) {
    
                //"Tuesday at 7:13 PM"
    
                [formatter setDateFormat:@"EEEE"];
                strDate = [formatter stringFromDate:date];
                [formatter setDateFormat:@"hh:mm a"];
                strDate2 = [formatter stringFromDate:date];
    
                timestamp = [NSString stringWithFormat:@"%@ at %@", strDate, strDate2];
    
            } else if (yearDifference == 0) {
    
                //"July 4 at 7:36 AM"
    
                [formatter setDateFormat:@"MMMM d"];
                strDate = [formatter stringFromDate:date];
                [formatter setDateFormat:@"hh:mm a"];
                strDate2 = [formatter stringFromDate:date];
    
                timestamp = [NSString stringWithFormat:@"%@ at %@", strDate, strDate2];
    
            } else {
    
                //"March 24 2010 at 4:50 AM"
    
                [formatter setDateFormat:@"d MMMM yyyy"];
                strDate = [formatter stringFromDate:date];
                [formatter setDateFormat:@"hh:mm a"];
                strDate2 = [formatter stringFromDate:date];
    
                timestamp = [NSString stringWithFormat:@"%@ at %@", strDate, strDate2];
            }
        }
    
        return timestamp;
    }
    

提交回复
热议问题