设置当前显示的日期:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSDate *date = [dateFormatter dateFromString:@"1970-01-01"];//一开始显示1970年
[_pikerView.datePickerView setDate:date];
// 1.日期Picker
UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, __kScreenWidth, __kDatePickerH)];
datePicker.backgroundColor = [UIColor whiteColor];
// 1.1选择datePickr的显示风格
[datePicker setDatePickerMode:UIDatePickerModeDate];
// 1.2查询所有可用的地区
//NSLog(@"%@", [NSLocale availableLocaleIdentifiers]);
// 1.3设置datePickr的地区语言, zh_Han后面是s的就为简体中文,zh_Han后面是t的就为繁体中文
[datePicker setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_Hans_CN"]];
// 1.4监听datePickr的数值变化
[datePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged];
NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSDate *date = [dateFormatter dateFromString:lastTime];//上次设置的日期
if (!lastTime) {
date = [NSDate date];
}
// 2.3 将转换后的日期设置给日期选择控件
[datePicker setDate:date];
NSDate * timeDate=[NSDate date];
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = @"yyyy-MM-dd";
NSDate *minDate = [fmt dateFromString:@"1930-1-1"];
//设置日期最大及最小值
datePicker.maximumDate = timeDate;
datePicker.minimumDate = minDate;
//设置字体颜色
[datePicker setValue:kColor3C3C3C forKeyPath:@"textColor"];
[self addSubview:datePicker];