本周,本月筛选方法

2016-09-01  本文已影响28人  Jabne

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);
上一篇下一篇

猜你喜欢

热点阅读