UIDatePicker 时间滚动表

时间:2020-12-20 15:28:48

UIDatePicker *datapicker; //时间滚动表

datapicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(50, 200, 250, 100)];

datapicker.datePickerMode = UIDatePickerModeDate;

NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];

datapicker.locale = locale;

NSDate *select = datapicker.date;

datapicker.maximumDate =select;

[datapicker addTarget:self action:@selector(agepicker) forControlEvents:UIControlEventValueChanged];

[informationView addSubview:datapicker];

agelabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 55, Kwidth, 25)];

agelabel.backgroundColor = [UIColor redColor];

agelabel.textAlignment = NSTextAlignmentLeft;

[informationView addSubview:agelabel];

#pragma mark - 得到年龄

-(void)agepicker{

NSDate *select = datapicker.date;

NSTimeInterval dateDiff = [select timeIntervalSinceNow];

int age=-trunc(dateDiff/(60*60*24))/365;//这是根据年月日计算出年龄的一个函数 我的计算出来时是一个负值 直接在前面加了一个负号 也没去管他

NSLog(@"年龄%d",age);

agelabel.text = [NSString stringWithFormat:@"%d",age];

这一是显示具体的年月日

NSDate *select = datapicker.date;

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setDateFormat:@"yyyy-MM-dd"];

NSString *dateAndTime = [dateFormatter stringFromDate:select];

label.text = [NSString stringWithFormat:@"%@",dateAndTime];//这是自己做地一个label来显现年月日

}