-(void)textFieldDidBeginEditing:(UITextField *)textField
{
[textField resignFirstResponder];
picker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 290, 320, 216)];
[picker setDatePickerMode:UIDatePickerModeDate];
picker.backgroundColor = [UIColor redColor];
self.txt_dob.inputView = picker;
[picker setMaximumDate:[NSDate date]];
format=[[NSDateFormatter alloc]init];
[format setDateFormat:@"dd/MM/YYYY"];
[self.view addSubview:picker];
UIToolbar *toolBar= [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,320,44)];
[toolBar setBarStyle:UIBarStyleBlackOpaque];
UIBarButtonItem *barButtonDone = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(showSelectedDate:)];
toolBar.items = @[barButtonDone];
barButtonDone.tintColor=[UIColor blackColor];
[picker addSubview:toolBar];
}
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MMM-yyyy"];
NSDate *startD = [dateFormatter dateFromString:@"15-Sep-1997"];
NSDate *endD = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
//NSUInteger unitFlags = NSCalendarUnitYear|NSCalendarUnitMonth|//NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond;
NSDateComponents *components = [calendar components:unitFlags fromDate:startD toDate:endD options:0];
NSInteger year = [components year];
NSInteger month = [components month];
NSInteger day = [components day];
NSLog(@"%ld:%ld:%ld", (long)year, (long)month,(long)day);
}
@end
Asked
Active
Viewed 71 times
-6

Viral Savaj
- 3,379
- 1
- 26
- 39

Sampri
- 1
-
2What is the error exactly... – Ashok Londhe Apr 29 '15 at 04:25
-
replay... if you are not replay then i don't getting your problem exactly. – Ashok Londhe Apr 29 '15 at 04:38
-
eerror on NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"dd-MMM-yyyy"]; NSDate *startD = [dateFormatter dateFromString:@"15-Sep-1997"]; NSDate *endD = [NSDate date]; NSCalendar *calendar = [NSCalendar currentCalendar]; – Sampri Apr 29 '15 at 09:19
-
i have check your code. it don't give any error. – Ashok Londhe Apr 29 '15 at 09:26
-
i want a function which calculate age from date picker programmatically. – Sampri Apr 29 '15 at 10:04
-
I have given date manually... you can select dateOf Birth from Date Picker and write below code. it will solve your problem. – Ashok Londhe Apr 29 '15 at 10:29
-
you have not replay anything... – Ashok Londhe Apr 29 '15 at 11:11
1 Answers
0
Try This to select date from date picker...
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
datePicker =[[UIDatePicker alloc]initWithFrame:CGRectMake(50,150,10, 50)];
datePicker.datePickerMode=UIDatePickerModeDate;
datePicker.hidden=NO;
datePicker.date=[NSDate date];
[datePicker addTarget:self action:@selector(textFieldTitle:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:datePicker];
UIBarButtonItem * rightBtn=[[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(save:)];
self.navigationItem.rightBarButtonItem=rightBtn;
return true;
}
-(void)textFieldTitle:(id)sender
{
NSDateFormatter *dateFormat=[[NSDateFormatter alloc]init];
dateFormat.dateStyle=NSDateFormatterMediumStyle;
[dateFormat setDateFormat:@"MM/dd/yyyy"];
NSString *str=[NSString stringWithFormat:@"%@",[dateFormat stringFromDate:datePicker.date]];
self.txtField.text=str;
}
-(void)save:(id)sender
{
self.navigationItem.rightBarButtonItem=nil;
[datePicker removeFromSuperview];
}
To calculate the date....
NSDate *today = [NSDate date];
NSString *birthdate=self.txtField.text;
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:@"dd-MM-yyyy"];
NSDate *myDate = [[NSDate alloc]init];
myDate=[dateFormatter1 dateFromString:birthdate];
NSDateComponents *ageComponents = [[NSCalendar currentCalendar]
components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay
fromDate:myDate
toDate:today
options:0];
NSLog(@"%@",ageComponents);

Ashok Londhe
- 1,491
- 11
- 29
-
but here as u say.......date is already provided....but i want the picker to select date from date picker programmatically when user click on a textfield...please provide me the answer. – Sampri Apr 30 '15 at 12:21
-
-
http://www.techotopia.com/index.php/Using_the_iOS_7_UIPickerView_and_UIDatePicker_Components – Ashok Londhe Apr 30 '15 at 13:54