获取本周本月上周上月

///获取某一周的开始和结束时间 lastNumber代表的是当前时间的前几周,0代表本周,-1上一周,1下一周
- (void)getWeekBeginAndEndWithNumbe:(NSInteger)lastNumber{
    NSDate * nowDate = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *comp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday  fromDate:nowDate];

    // 获取今天是周几
    NSInteger weekDay = [comp weekday];
    /**获取当前几个月*/
    NSInteger monthDay = [comp month];
    NSLog(@"%ld",monthDay);

    // 获取几天是几号
    NSInteger day = [comp day];
    // 计算当前日期和本周的星期一和星期天相差天数
    long firstDiff,lastDiff;
    if (weekDay == 1){
        firstDiff = -6;
        lastDiff = 0;
    } else{
        firstDiff = [calendar firstWeekday] - weekDay + 1;
        lastDiff = 8 - weekDay;
    }
    //开始时间
    NSDateComponents *begainDayComp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay  fromDate:nowDate];
    [begainDayComp setDay:day + firstDiff + 7*lastNumber];//在当前日期(去掉时分秒)基础上加上差的天数
    NSDate *begain = [calendar dateFromComponents:begainDayComp];
    //结束时间
    NSDateComponents *endDayComp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay   fromDate:nowDate];
    [endDayComp setDay:day + lastDiff + 7*lastNumber];//在当前日期(去掉时分秒)基础上加上差的天数
    NSDate *endDayOfWeek = [calendar dateFromComponents:endDayComp];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString *firstDay = [formatter stringFromDate:begain];
    NSString *lastDay = [formatter stringFromDate:endDayOfWeek];
    NSString *dateStr = [NSString stringWithFormat:@"%@-%@",firstDay,lastDay];
    NSLog(@"某周:%@",dateStr);
}
///获取某一月的开始和结束时间 lastMonth代表的是当前时间的前几月,0代表本月,-1上一月,1下一月
- (void)getMonthBeginAndEndWith:(NSInteger )lastMonth{
    NSDate * newDate = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    //设置新的日期
    NSDateComponents *comp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday  fromDate:newDate];
    [comp setMonth:comp.month+lastMonth];
    newDate = [calendar dateFromComponents:comp];
    
    double interval = 0;
    NSDate *beginDate = nil;
    NSDate *endDate = nil;
    [calendar setFirstWeekday:2];
    //设定周一为周首日
    BOOL ok = [calendar rangeOfUnit:NSCalendarUnitMonth startDate:&beginDate interval:&interval forDate:newDate]; //分别修改为 NSDayCalendarUnit NSWeekCalendarUnit NSYearCalendarUnit
    if (ok) {
        endDate = [beginDate dateByAddingTimeInterval:interval-1];
    }else {
        return;
    }
    
    NSDateFormatter *myDateFormatter = [[NSDateFormatter alloc] init];
    [myDateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *beginString = [myDateFormatter stringFromDate:beginDate];
    NSString *endString = [myDateFormatter stringFromDate:endDate];
    NSString *s = [NSString stringWithFormat:@"%@-%@",beginString,endString];
    NSLog(@"某月:%@",s);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值