IOS比较两个日期的大小获取当前月份天数(今天、明天、后天)

项目中有一个时间选择期,需要显示今天、明天、后天。对于需要跨月的情况要对日期本月天数进行判断。

/** 获取当前月份的最后一天 */
-(void)_getDateBtnValue
{
    NSInteger year = [[NSDate date] getYear]; //获取年份
    NSInteger month = [[NSDate date] getMonth];//获取月份
    NSInteger day = [[NSDate date] getDay];//获取日期
    
    //判断当前月分的天数
    int endDate = 0;
    switch (month) {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            endDate = 31;
            break;
            break;
        case 4:
        case 6:
        case 9:
        case 11:
            endDate = 30;
            break;
        case 2:
            // 是否为闰年
            if (year % 400 == 0) {
                endDate = 29;
                break;
            } else {
                if (year % 100 != 0 && year %4 ==4) {
                    endDate = 29;
                } else {
                    endDate = 28;
                }
                break;
            }
            break;
        default:
            break;
    }
    
    NSDictionary *dic =  [self calculateDateWithEndDate:endDate withDay:day withMonth:month];
    dateDic = dic;
    //更新Button内容
    [self.todayBtn setTitle:[NSString stringWithFormat:@"今天(%@/%@)",dic[@"month"],dic[@"day"]] forState:UIControlStateNormal];
    [self.tomorrowBtn setTitle:[NSString stringWithFormat:@"明天(%@/%@)",dic[@"tomorrowMonth"],dic[@"tomorrow"]] forState:UIControlStateNormal];
    [self.afterDayBtn setTitle:[NSString stringWithFormat:@"后天(%@/%@)",dic[@"afterDayMonth"],dic[@"afterDay"]] forState:UIControlStateNormal];
}

/**
 *  判断当前今天明天后天在跨月的时候天数和月份数
 *  @parma endDate:当月最后一天
 *  @parma day:今天的日期
 *  @parma month:当前月份
 *  @parma weekday:当前是周几
 */

-(NSDictionary *)calculateDateWithEndDate:(int)endDate withDay:(NSInteger)day withMonth:(NSInteger)month
{
    NSInteger tomorrow = 0; //明天
    NSInteger afterDay = 0;  //后天
    NSInteger tomorrowMonth = 0;
    NSInteger afterDayMonth = 0;
    NSInteger currentYear = [[NSDate date] getYear];
    NSInteger nextYear = currentYear;
    NSInteger lastYear = nextYear;
    
    if(endDate - day <= 0)
    {
        tomorrow = 1;
        afterDay = tomorrow + 1;
        tomorrowMonth = month + 1;
        if(tomorrowMonth > 12)
        {
            tomorrowMonth = 1;
            nextYear=nextYear + 1;
            lastYear = nextYear;
        }
        afterDayMonth = tomorrowMonth;
    }
    
    else if(endDate - day == 1)
    {
        tomorrow = endDate;
        afterDay = 1;
        tomorrowMonth = month;
        afterDayMonth = month + 1;
        if(month == 12)
        {
            afterDayMonth = 1;
            lastYear = nextYear + 1;
        }
    }
    
    else
    {
        tomorrow = day+1;
        afterDay = tomorrow + 1;
        tomorrowMonth = month;
        afterDayMonth = month;
    }
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:[NSNumber numberWithInteger:day] forKey:@"day"];
    [dic setObject:[NSNumber numberWithInteger:month] forKey:@"month"];
    [dic setObject:[NSNumber numberWithInteger:currentYear] forKey:@"year"];
    
    [dic setObject:[NSNumber numberWithInteger:tomorrow] forKey:@"tomorrow"];
    [dic setObject:[NSNumber numberWithInteger:tomorrowMonth] forKey:@"tomorrowMonth"];
    [dic setObject:[NSNumber numberWithInteger:nextYear] forKey:@"nextYear"];
    
    [dic setObject:[NSNumber numberWithInteger:afterDay] forKey:@"afterDay"];
    [dic setObject:[NSNumber numberWithInteger:lastYear] forKey:@"lastYear"];
    [dic setObject:[NSNumber numberWithInteger:afterDayMonth] forKey:@"afterDayMonth"];
    
    return dic;
    
}

/**
 *  判断两个时间的大小
 *  @parma selectDate:选择的日期
 *  @return 1:大于当前日期 -1:小于当前时间 0:等于当前时间
 */
-(int)compareDateWithSelectDate:(NSString *)selectDate
{
    NSDate *currentDate = [NSDate date];
    NSDateFormatter *fomatter = [[NSDateFormatter alloc] init];
    [fomatter setDateFormat:@"YYYY-MM-dd HH:mm"];
    NSString *curentDateStr = [fomatter stringFromDate:currentDate];
    currentDate = [fomatter dateFromString:curentDateStr];
    
    int result = [[fomatter dateFromString:selectDate] compare:currentDate];
    if(result == NSOrderedDescending)
    {
        return 1;
    }
    else if(result == NSOrderedAscending)
    {
        return -1;
    }
    return 0;
}

/** 获取今天是周几 **/
-(NSString *)_getWeakDay:(UIButton *)sender
{
    NSInteger weakday = [[NSDate date] weekday];//周末为第一天
    NSString *selectWeakDay = @"";
    NSInteger tmpWeakday = weakday;
    switch (sender.tag) {
        case 100://今天
        {
            tmpWeakday = weakday;
        }
            break;
        case 101://明天
        {
            tmpWeakday = weakday+1;
            if(tmpWeakday>7)
            {
                tmpWeakday = 1;
            }
        }
            break;
        case 102://后天
        {
            tmpWeakday = weakday + 2;
            if(tmpWeakday - 7 > 0)
            {
                (tmpWeakday == 8) ? (tmpWeakday = 1) : (tmpWeakday = 2);
            }
        }
            break;
        default:
            break;
    }
    
    switch (tmpWeakday) {
        case 1:
            selectWeakDay = @"星期天";
            break;
            
        case 2:
            selectWeakDay = @"星期一";
            break;
            
        case 3:
            selectWeakDay = @"星期二";
            break;
            
        case 4:
            selectWeakDay = @"星期三";
            break;
            
        case 5:
            selectWeakDay = @"星期四";
            break;
            
        case 6:
            selectWeakDay = @"星期五";
            break;
            
        case 7:
            selectWeakDay = @"星期六";
            break;
        default:
            break;
    }
    
    return selectWeakDay;
}


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/cjh16102377/article/details/47022785
文章标签: ios
个人分类: ios开发
上一篇IOS NSUserDefalut存储位置
下一篇IOS定时器
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭