美文网首页
本周,本月筛选方法

本周,本月筛选方法

作者: Jabne | 来源:发表于2016-09-01 17:08 被阅读28次

    NSDate* today = [NSDate date];
    double intervalweek = 0;
    double intervalmonth = 0;
    NSDate *WeekbeginDate = nil;
    NSDate *WeekendDate = nil;
    NSDate *MonthbeginDate = nil;
    NSDate *MonthendDate = nil;

        NSCalendar *calendar = [NSCalendar currentCalendar];
        [calendar setFirstWeekday:2];//设定周一为周首日
        BOOL ok = [calendar rangeOfUnit:NSCalendarUnitWeekOfMonth startDate:&WeekbeginDate interval:&intervalweek forDate:today];
    
        BOOL ok2 = [calendar rangeOfUnit:NSCalendarUnitMonth startDate:&MonthbeginDate interval:&intervalmonth forDate:today];
        //分别修改为 NSDayCalendarUnit NSWeekCalendarUnit NSYearCalendarUnit
        if (ok) {
            WeekendDate = [WeekbeginDate dateByAddingTimeInterval:intervalweek-1];
        }else{
            return nil;
        }
    
       if (ok2) {
        MonthendDate = [MonthbeginDate dateByAddingTimeInterval:intervalmonth-1];
       }else {
        return nil;
       }
    
        NSDateFormatter *myDateFormatter = [[NSDateFormatter alloc] init];
        [myDateFormatter setDateFormat:@"yyyy-MM-dd EEEE"];
    
    NSString *WeekbeginString = [myDateFormatter stringFromDate:WeekbeginDate];
    NSString *WeekendString = [myDateFormatter stringFromDate:WeekendDate];
    NSString* MonthbeginString = [myDateFormatter stringFromDate:MonthbeginDate];
    NSString* MonthendString = [myDateFormatter stringFromDate:MonthendDate];
    
    BINLog(@"本周开始结束%@-%@ 本月开始结束%@-%@",WeekbeginString,WeekendString,MonthbeginString,MonthendString);

    相关文章

      网友评论

          本文标题:本周,本月筛选方法

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