任何人解决我从网络服务获得日期和时间匹配的问题,但我需要在日期选择器中编辑单独的时间...我需要设置最大时间和最短时间。
这是我的代码
- (IBAction)EndBreak_btn:(id)sender {
// _text_EndBreak.text=_MATCHDATE;
[_datePicker_View setHidden:NO];
[_date_picker setHidden:YES];
[_date_picker1 setHidden:NO];
_date_picker1.datePickerMode = UIDatePickerModeTime;
[_date_picker1 addTarget:self
action:@selector(BreakEnd:)forControlEvents:UIControlEventValueChanged];
}
-(void)BreakEnd:(id)sender
{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"hh:mm:ss"];
NSDate *date = [dateFormat dateFromString:_MATCHDATE];
[dateFormat setDateFormat:@"hh:mm:ss"];
[_date_picker1 setDate:date];
//[_date_picker1 setMinimumDate:date];
NSString *Time = [dateFormat stringFromDate:date];
BREAKENDTIME =[NSString stringWithFormat:@"%@ %@" ,[_text_EndBreak text],Time];
}
我在时间选择器中单独显示时间但是当我选择它因为MatchDate字符串是Nill而崩溃时...我无法从MatchDateString单独转换时间 在此先感谢
答案 0 :(得分:1)
使用
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
2016-06-25 12:00:00
[dateFormat setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
NSDate *date = [dateFormat dateFromString:_MATCHDATE];
[dateFormat setDateFormat:@"hh:mm:ss"];
// for minimum date
[_date_picker1 setMinimumDate:date];
// for maximumDate
int daysToAdd = 2;
NSDate *newDate1 = [date dateByAddingTimeInterval:60*60*24*daysToAdd];
[_date_picker1 setMaximumDate:newDate1];
NSString *Time = [dateFormat stringFromDate:date];
BREAKENDTIME =[NSString stringWithFormat:@"%@ %@" ,[_text_EndBreak text],Time];