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];
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值