fathima fathima - 5 months ago 9
iOS Question

Showing Time in Date Picker Set The Maximum time And Minimum time

Any One pls Solve the Issues I got The Date And Time MATCHDATE from web service but i Need to edit the Time Alone in Date Picker ...And I need To set the Maximum Time And Minimum Time.

Here Is my Code

- (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];

}


i show the Time alone in Time Picker But When I select th time It getting Crashed because of the MatchDate String is Nill ...I can't convert the Time Alone From The MatchDateString
Thanks In Advance

Answer

use like

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];