• iOS:判断昨天,今天,今年


     

    - (BOOL)isThisYear
    {
        // 日历
        NSCalendar *calendar = [NSCalendar currentCalendar];
        
        NSInteger nowYear = [calendar component:NSCalendarUnitYear fromDate:[NSDate date]];
        NSInteger selfYear = [calendar component:NSCalendarUnitYear fromDate:self];
        
        return nowYear == selfYear;
    }
    
    //- (BOOL)isToday
    //{
    //    // 日历
    //    NSCalendar *calendar = [NSCalendar currentCalendar];
    //    
    //    NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
    //    
    //    NSDateComponents *nowCmps = [calendar components:unit fromDate:[NSDate date]];
    //    NSDateComponents *selfCmps = [calendar components:unit fromDate:self];
    //    
    //    return nowCmps.year == selfCmps.year
    //    && nowCmps.month == selfCmps.month
    //    && nowCmps.day == selfCmps.day;
    //}
    
    - (BOOL)isToday
    {
        NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
        fmt.dateFormat = @"yyyy-MM-dd";
        
        NSString *nowString = [fmt stringFromDate:[NSDate date]];
        NSString *selfString = [fmt stringFromDate:self];
        
        return [nowString isEqualToString:selfString];
    }
    
    - (BOOL)isYesterday
    {
        // 2014-12-31 23:59:59 -> 2014-12-31
        // 2015-01-01 00:00:01 -> 2015-01-01
        
        // 日期格式化类
        NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
        fmt.dateFormat = @"yyyy-MM-dd";
        
        NSDate *nowDate = [fmt dateFromString:[fmt stringFromDate:[NSDate date]]];
        NSDate *selfDate = [fmt dateFromString:[fmt stringFromDate:self]];
        
        NSCalendar *calendar = [NSCalendar currentCalendar];
        NSDateComponents *cmps = [calendar components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:selfDate toDate:nowDate options:0];
        
        return cmps.year == 0
        && cmps.month == 0
        && cmps.day == 1;
    }

     发帖时间的判断:

    - (NSString *)create_time
    {
        // 日期格式化类
        NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
        // 设置日期格式(y:年,M:月,d:日,H:时,m:分,s:秒)
        fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
        // 帖子的创建时间
        NSDate *create = [fmt dateFromString:_create_time];
        
        if (create.isThisYear) { // 今年
            if (create.isToday) { // 今天
                NSDateComponents *cmps = [[NSDate date] deltaFrom:create];
                
                if (cmps.hour >= 1) { // 时间差距 >= 1小时
                    return [NSString stringWithFormat:@"%zd小时前", cmps.hour];
                } else if (cmps.minute >= 1) { // 1小时 > 时间差距 >= 1分钟
                    return [NSString stringWithFormat:@"%zd分钟前", cmps.minute];
                } else { // 1分钟 > 时间差距
                    return @"刚刚";
                }
            } else if (create.isYesterday) { // 昨天
                fmt.dateFormat = @"昨天 HH:mm:ss";
                return [fmt stringFromDate:create];
            } else { // 其他
                fmt.dateFormat = @"MM-dd HH:mm:ss";
                return [fmt stringFromDate:create];
            }
        } else { // 非今年
            return _create_time;
        }
    }

     

     
  • 相关阅读:
    macOS下iTerm2+zsh+oh-my-zsh+powerlevel10k打造最强终端
    个别服务器通过公网登录MySQL数据库慢
    Linux永久添加静态路由
    mtr网络工具常用命令
    Fping常用的网络连通性测试技巧
    Linux 7修改网卡名称后配置文件中的默认网关不生效
    Mac TimeMachine备份数据到自建NAS(通过samba共享)
    Mac iTerm2使用lrzsz
    SSH登录服务器慢
    Linux服务器惨遭挖矿
  • 原文地址:https://www.cnblogs.com/yintingting/p/4549351.html
Copyright © 2020-2023  润新知