//日期选择器
@property (nonatomic,strong)UIDatePicker *myDatePicker;
//记录当前选中时间
@property (nonatomic,strong)NSString *recordTime;
//懒加载
[self.View addSubview:self.myDatePicker];
//初始化
- (UIDatePicker *)myDatePicker
{
if (!_myDatePicker) {
_myDatePicker = [[UIDatePicker alloc]initWithFrame:DrawingAdapter(0, 51, 375, 367)];
NSTimeInterval secondPerDay = 24 * 60 * 60;
NSTimeInterval secondPer2Year = secondPerDay * 356;
NSDate *today = [[NSDate alloc]init];
NSDate *min = [today dateByAddingTimeInterval:0];//最小时间
NSDate *max = [today dateByAddingTimeInterval:secondPer2Year];//最大时间
_myDatePicker.minuteInterval = 10;//每10分一跳
[_myDatePicker setLocale:[[NSLocale alloc] initWithLocaleIdentifier: @"zh_CN"]];
[_myDatePicker setDatePickerMode:UIDatePickerModeDate];//选择器模式
[_myDatePicker setDate:today animated:YES];
[_myDatePicker setMinimumDate:min];
[_myDatePicker setMaximumDate:max];
[_myDatePicker addTarget:self action:@selector(onDatePickerChanged:) forControlEvents:UIControlEventValueChanged];
}
return _myDatePicker;
}
//选择器调用
- (void)onDatePickerChanged:(UIDatePicker *)datePicker {
NSDate *select = datePicker.date;
NSDateFormatter *selectDateFormatter = [[NSDateFormatter alloc]init];
selectDateFormatter.dateFormat = @"yyyy-MM-dd HH:hh:ss";
NSString *dateAndTime = [selectDateFormatter stringFromDate:select];
_recordTime = dateAndTime;
}
//模式分类
1、UIDatePickerModeTime
2、UIDatePickerModeDate
3、UIDatePickerModeDateAndTime