nsdate 前一天,后一天

h
{
    NSDate *choseDate;//显示日期
    NSDateFormatter *df;//时间格式化
    NSCalendar*calendar;//日历
    NSDateComponents*comps;//
}
@property (weak, nonatomic) IBOutlet UIButton *dateL;
@property (weak, nonatomic) IBOutlet UIButton *beforeBtn;
@property (weak, nonatomic) IBOutlet UIButton *nextBtn;

m
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    df = [[NSDateFormatter alloc]init];
    [df setDateFormat:@"yyy-MM-dd"];
    calendar = [NSCalendar currentCalendar];
     
    [self _initCDate:[NSDate date]];
    [_beforeBtn addTarget:self action:@selector(beforeAc) forControlEvents:UIControlEventTouchUpInside];
    [_nextBtn addTarget:self action:@selector(nextAc) forControlEvents:UIControlEventTouchUpInside];
}

#pragma maek date
-(void)_initCDate:(NSDate *)_nsdate{
    choseDate=_nsdate;
    comps =[calendar components:(NSWeekCalendarUnit | NSWeekdayCalendarUnit |NSWeekdayOrdinalCalendarUnit)fromDate:choseDate];
    
    NSInteger weekday = [comps weekday]; // 星期几(注意,周日是“1”,周一2………………)
    NSString *wdS ;
    switch (weekday) {
        case 1:
            wdS=@"日";
            break;
        case 2:
            wdS=@"一";
            break;
        case 3:
            wdS=@"二";
            break;
        case 4:
            wdS=@"三";
            break;
        case 5:
            wdS=@"四";
            break;
        case 6:
            wdS=@"五";
            break;
        case 7:
            wdS=@"六";
            break;
        default:
            break;
    }
    
    NSString *date_= [df stringFromDate:choseDate];
    [_dateL setTitle:[NSString stringWithFormat:@" %@ 星期%@ ",date_,wdS] forState:UIControlStateNormal];
    _dateL.userInteractionEnabled=NO;
}

#pragma mark 前一天Ac
-(void)beforeAc{
    choseDate=[[NSDate alloc]initWithTimeIntervalSinceReferenceDate:([choseDate timeIntervalSinceReferenceDate]-24*3600)];
    [self _initCDate:choseDate];
}
#pragma mark 后一天Ac
-(void)nextAc{
    choseDate=[[NSDate alloc]initWithTimeIntervalSinceReferenceDate:([choseDate timeIntervalSinceReferenceDate]+24*3600)];
    [self _initCDate:choseDate];
}

你可能感兴趣的:(NSDate)