首页 > 代码库 > 根据日期计算发布时间段(NSCalendar)

根据日期计算发布时间段(NSCalendar)

<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #008400 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #703daa } p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #d12f1b } p.p5 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px } p.p6 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #3d1d81 } span.s1 { } span.s2 { font: 11.0px "PingFang SC" } span.s3 { color: #703daa } span.s4 { color: #000000 } span.s5 { color: #3d1d81 } span.s6 { color: #bb2ca2 } span.s7 { color: #008400 } span.s8 { font: 11.0px "PingFang SC"; color: #008400 } span.s9 { color: #272ad8 } span.s10 { color: #d12f1b } span.s11 { font: 11.0px "PingFang SC"; color: #d12f1b }</style> <style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #008400 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #703daa } p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #d12f1b } p.p5 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px } p.p6 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #3d1d81 } span.s1 { } span.s2 { font: 11.0px "PingFang SC" } span.s3 { color: #703daa } span.s4 { color: #000000 } span.s5 { color: #3d1d81 } span.s6 { color: #bb2ca2 } span.s7 { color: #008400 } span.s8 { font: 11.0px "PingFang SC"; color: #008400 } span.s9 { color: #272ad8 } span.s10 { color: #d12f1b } span.s11 { font: 11.0px "PingFang SC"; color: #d12f1b }</style>

// 返回发布时间dateWithString

- (NSString *)backReleaseTimeWithDateStr:(NSString *)dateWithString{

    // 获取当前NSCalendar

    NSCalendar *calendar = [NSCalendar currentCalendar];

    // 设置日期格式

    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];

    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";

    //入参 发布日期

    NSDate *releaseTimer = [formatter dateFromString:dateWithString];

    

    if ([calendar isDateInToday:releaseTimer]) { // 今天

        //当前时间

        NSDate *nowDate = [NSDate date];

        // 获得日期之间的间隔

        NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;

        NSDateComponents *components = [calendar components:unit fromDate:releaseTimer toDate:nowDate options:0];

        

        if (components.hour >= 1) {

//            formatter.dateFormat = @"今天 HH:mm";

//            return [formatter stringFromDate:releaseTimer];

            return [NSString stringWithFormat:@"%zd小时前",components.hour];

        }else if(components.minute >= 1){

            return [NSString stringWithFormat:@"%zd分钟前",components.minute];

        }else{

            return @"刚刚";

        }

    }else if([calendar isDateInYesterday:releaseTimer]){  // 昨天

        formatter.dateFormat = @"昨天 HH:mm";

        return [formatter stringFromDate:releaseTimer];

    }else// 其他

        formatter.dateFormat = @"MM-dd HH:mm";

        return [formatter stringFromDate:releaseTimer];

    }

    return dateWithString;

}

根据日期计算发布时间段(NSCalendar)