Reputation: 21
I have written following piece of code to bring a pickerView foe selecting time. which is working perfectly now i'm wondering how can I set a range of time between 9AM and 5 PM. Please can someone help me in achieving the requirement of limiting the time selection between 9 AM - 5 PM.
@interface CleaningDetails ()<UITextFieldDelegate>
{
IBOutlet TextFieldValidator *cleanTime;
UIDatePicker *time;
}
@implementation
time = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
time.datePickerMode = UIDatePickerModeTime;
time.minuteInterval = 30;
// set change the inputView (default is keyboard) to UIPickerView
[self.cleanTime setInputView:time];
// add a toolbar with Cancel & Done button
UIToolbar *toolBar1 = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
toolBar1.barStyle = UIBarStyleBlackOpaque;
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(showSelectedTime)];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelTouched:)];
// the middle button is to make the Done button align to right
[toolBar1 setItems:[NSArray arrayWithObjects:cancelButton, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], doneButton, nil]];
[self.cleanTime setInputAccessoryView:toolBar1];
I'm really new to this programming and objective-C specially can anyone suggest me some sources or any idea to get this requirement done.
Upvotes: 0
Views: 649
Reputation: 3245
try this code,
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
startTime.text = [NSString stringWithFormat:@"%@",[self beginningOfDay:[NSDate new]]];
endTime.text = [NSString stringWithFormat:@"%@",[self endOfDay:[NSDate new]]];
}
-(NSDate *)beginningOfDay:(NSDate *)date
{
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:( NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:date];
[components setHour:9];
[components setMinute:0];
[components setSecond:0];
return [cal dateFromComponents:components];
}
-(NSDate *)endOfDay:(NSDate *)date
{
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:date];
[components setHour:17];
[components setMinute:0];
[components setSecond:0];
return [cal dateFromComponents:components];
}
you will get the return time, then convert UTC time to local time,
hope its helpful.
Upvotes: 0
Reputation: 3245
UIDatePicker support minimum and maximum date only, not a time. but you will set the time using time interval, use this below code and set your timing,
NSDate *minDate = [NSDate new];
// One hour from now
NSDate *maxDate = [[NSDate alloc] initWithTimeIntervalSinceNow:60*60];
UIDatePicker *picker = [UIDatePicker new];
[picker setDatePickerMode:UIDatePickerModeDateAndTime];
[picker setMinimumDate:minDate];
[picker setMaximumDate:maxDate];
hope its helpful
Upvotes: 2