美文网首页
判断日期是否是昨天、今天、明天、后天

判断日期是否是昨天、今天、明天、后天

作者: MJBaby | 来源:发表于2019-07-31 15:20 被阅读0次

    由于项目需要,整理出来以供大家参考,请各位大神指教,多谢🤝

    方法一:NSCalendar 自带方法判断是否是昨天、今天、明天

    + (NSString *)dateStrToStrWithDateStr:(NSString *)dateStr {
        NSString *flagString = @"";
        // 目标日期
        NSDate *tarDate = [self getDateFromDateStr:dateStr];
        if ([[NSCalendar currentCalendar] isDateInYesterday:tarDate]) {
            flagString = @"昨天";
        }else if ([[NSCalendar currentCalendar] isDateInToday:tarDate]) {
            flagString = @"今天";
        }else if ([[NSCalendar currentCalendar] isDateInTomorrow:tarDate]) {
            flagString = @"明天";
        }
        return flagString;
    }      
    

    方法二:只适用于判断同一个月份的昨天、今天、明天、后天

    
    + (NSString *)dateStrToStrWithNowDateStr:(NSString *)nowDateStr dateStr:(NSString *)dateStr {
        NSString *flagString = @"";
        // 当前日期
        int currentDateInt = [self getIntegerFromDateStr:nowDateStr];
        // 目标日期
        int tarInt = [self getIntegerFromDateStr:dateStr];
        // 时间差
        int diff = tarInt - currentDateInt;
        
        if ( diff < 3 && diff >= 0) {
            switch (diff) {
                case 0:
                    flagString = @"今天";
                    break;
                case 1:
                    flagString = @"明天";
                    break;
                case 2:
                    flagString = @"后天";
                    break;
                default:
                    break;
            }
        }else if (diff == -1) {
            flagString = @"昨天";
        }
        return flagString;
    }
    

    方法三:获取到当前日期的昨天、今天、明天、后天。。。来跟目标日期进行判断,适用于同一个月份和不同月份

    + (NSString *)dateStrToStrWithNowDateStr:(NSString *)nowDateStr dateStr:(NSString *)dateStr {
        NSString *flagString = @"";
        ///
        if ([dateStr isEqualToString:[self getTheDayAfterDay:nowDateStr addDay:-1]]) {
            flagString = @"昨天";
        }else if ([dateStr isEqualToString:[self getTheDayAfterDay:nowDateStr addDay:0]]) {
            flagString = @"今天";
        }else if ([dateStr isEqualToString:[self getTheDayAfterDay:nowDateStr addDay:1]]) {
            flagString = @"明天";
        }else if ([dateStr isEqualToString:[self getTheDayAfterDay:nowDateStr addDay:2]]) {
            flagString = @"后天";
        }
        return flagString;
    }
    + (NSString *)getTheDayAfterDay:(NSString *)theDayStr addDay:(int)addDay {
        NSDateFormatter *format = [[NSDateFormatter alloc]init];
        [format setDateFormat:@"yyyy-MM-dd"];
        NSDate *theDay = [format dateFromString:theDayStr];
        NSCalendar *cal = [NSCalendar currentCalendar];
        unsigned int time = NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay;
        NSDateComponents *t = [cal components:time fromDate:theDay];
        int year = (int)[t year];
        int month = (int)[t month];
        int day = (int)[t day];
        day = day + (addDay);
        int monthDays = [self daysCountOfMonth:month andYear:year];
        if (day > monthDays) {
            month += 1;
            day = day - monthDays;
        }else {
            ///不做任何处理
        }
        NSString *resultStr = [NSString stringWithFormat:@"%02d-%02d-%02d",year,month,day];
        return resultStr;
    }
    + (int)daysCountOfMonth:(NSInteger)month andYear:(NSInteger)year {
        if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
            return 31;
        }else if (month == 4 || month == 6 || month == 9 || month == 11) {
            return 30;
        }
        if((year%4 != 0) || (year%100 == 0 && year%400 != 0)) {
            return 28;
        }
        return 29;
    }
    

    方法四:综合上述所有方法的判断

    + (NSString *)dateStrToStrWithNowDateStr:(NSString *)nowDateStr dateStr:(NSString *)dateStr {
        NSString *flagString = @"";
        // 当前日期
        int currentDateInt = [self getIntegerFromDateStr:nowDateStr];
        // 目标日期
        int tarInt = [self getIntegerFromDateStr:dateStr];
        NSDate *tarDate = [self getDateFromDateStr:dateStr];
        // 时间差
        int diff = tarInt - currentDateInt;
        
        if ( diff < 3 && diff >= 0) {
            switch (diff) {
                case 0:
                    flagString = @"今天";
                    break;
                case 1:
                    flagString = @"明天";
                    break;
                case 2:
                    flagString = @"后天";
                    break;
                default:
                    break;
            }
        }else if (diff == -1) {
            flagString = @"昨天";
        }else {
            if ([[NSCalendar currentCalendar] isDateInYesterday:tarDate]) {
                flagString = @"昨天";
            }else if ([[NSCalendar currentCalendar] isDateInToday:tarDate]) {
                flagString = @"今天";
            }else if ([[NSCalendar currentCalendar] isDateInTomorrow:tarDate]) {
                flagString = @"明天";
            }else if ([dateStr isEqualToString:[self getTheDayAfterDay:nowDateStr addDay:2]]) {
                flagString = @"后天";
            }
        }
        return flagString;
    }
    
    其中公共方法如下:
    + (NSDate *)getDateFromDateStr:(NSString *)dateStr {
        NSDateFormatter *format = [[NSDateFormatter alloc]init];
        [format setDateFormat:@"yyyy-MM-dd"];
        NSDate *date = [format dateFromString:dateStr];
        return date;
    }
    + (int)getIntegerFromDateStr:(NSString *)dateStr {
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyyy-MM-dd"];
        NSDate *date = [formatter dateFromString:dateStr];
        NSDateFormatter *formatter1 = [[NSDateFormatter alloc]init];
        [formatter1 setDateFormat:@"yyyyMMdd"];
        NSString *todayStr = [formatter1 stringFromDate:date];
        int presentDay = [todayStr intValue];
        return presentDay;
    }
    

    相关文章

      网友评论

          本文标题:判断日期是否是昨天、今天、明天、后天

          本文链接:https://www.haomeiwen.com/subject/vyqtdctx.html