UIDatePicker Output - ios

I have attached the UITextField to the UIDatePicker for month, day, year only. When I select the date the text output in the UITextField displays the date and time still. How do i get the UItextfield to display only the month, day, and year? Thanks in advance for any help.
Here is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UIDatePicker *datePicker = [[UIDatePicker alloc]init];
[datePicker setDate:[NSDate date]];
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker addTarget:self action:#selector(updateTextField:) forControlEvents:UIControlEventValueChanged];
[self.dateField setInputView:datePicker];
[datePicker addTarget:self action:#selector(updateTextField:) forControlEvents:UIControlEventValueChanged];
}
-(void)updateTextField:(id)sender
{
UIDatePicker *picker = (UIDatePicker*)self.dateField.inputView;
self.dateField.text = [NSString stringWithFormat:#"%#",picker.date];
}

try this..
-(void)updateTextField:(id)sender
{
UIDatePicker *picker = (UIDatePicker*)self.dateField.inputView;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM dd yyyy"];
self.dateField.text = [dateFormat stringFromDate:picker.date];
}

Related

Displaying current default date in UIDatePickerr view

If I am selecting date from date picker view for current date, the time is also getting selected and displaying the current time, I want to separate in two different selection event
Here is my code snippet-
/* Button for displaying UIDatePicker view */
-(void) dueDateChanged:(UIDatePicker *)sender {
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd MMM, yyyy"];
self.lbl_StartDateValue.text = [dateFormatter stringFromDate:[sender date]];
self.planStartDate = [dateFormatter stringFromDate:[sender date]];
NSDate *date = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:[dateFormatter dateFromString:[dateFormatter stringFromDate:[sender date]]] options:0];
self.lbl_EndDateValue.text = [NSString stringWithFormat:#"End %#",[dateFormatter stringFromDate:date]];
self.planEndDate = [dateFormatter stringFromDate:date];
}
// Here I am not able to differentiate the selection of two picker view
-(void)onDoneButtonClick {
[self dueDateChanged:self.picker];
[self dueTimeChanged:self.picker];
[self.toolbar removeFromSuperview];
[self.picker removeFromSuperview];
[self.tempBackgroundView removeFromSuperview];
}
-(void) dueTimeChanged:(UIDatePicker *)sender {
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"hh:mm a"];
self.lbl_timeValue.text = [dateFormatter stringFromDate:[sender date]];
self.planAlarmtime = [dateFormatter stringFromDate:[sender date]];
}
do like , initially set the current date on your text field whenever user present the date picker
(IBAction)btn_calendar_event:(id)sender {
......
[self.toolbar sizeToFit];
[self.view addSubview:self.toolbar];
//if you want to show the default date as current date
self.picker.date = [NSDate date];
// set inital date on your textfield
[self dueDateChanged : self.picker];
}
if user pressed the done button then call this
-(void)onDoneButtonClick {
// call the method
[self dueDateChanged : self.picker];
[self.toolbar removeFromSuperview];
[self.picker removeFromSuperview];
[_txtf_birthDate resignFirstResponder];
}
finally change the YYYY format
-(void) dueDateChanged:(UIDatePicker *)sender {
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSLog(#"Picked the date %#", [dateFormatter stringFromDate:[sender date]]);
_txtf_birthDate.text = [dateFormatter stringFromDate:[sender date]];
}
Just set minimum data as your current date
[self.picker setMinimumDate:[NSDate date]];
also you will not select until you scroll Picker
if you want to show the default date as current date
self.picker.date = [NSDate date]
this is the code for UIDatepicker alloca and set current date
UIDatePicker *datepicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height-320, self.view.frame.size.width, 320)];
datepicker.date = [NSDate date];
[self.view addSubview:datepicker];

how to show date in textfield from date picker?

After Clicked...Date is not shown in textfield.
Textfield is in custom cell of tableview.
UIDatePicker *datePicker = [[UIDatePicker alloc]init];
[datePicker setDate:[NSDate date]];
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker addTarget:self action:#selector(dateTextField:) forControlEvents:UIControlEventValueChanged];
[txtField_date setInputView:datePicker];
-(void) dateTextField:(id)sender
{
UIDatePicker *picker = (UIDatePicker*)txtField_date.inputView;
[picker setMaximumDate:[NSDate date]];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSDate *eventDate = picker.date;
[dateFormat setDateFormat:#"dd/MM/yyyy"];
NSString *dateString = [dateFormat stringFromDate:eventDate];
txtField_date.text = [NSString stringWithFormat:#"%#",dateString];
}
Try this code.
You can use the date property on UIDatePicker and convert it to NSString and set it to your UITextField.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy"]; // set your own format
NSString *formattedDateString = [formatter stringFromDate:yourPicker.date];
textField.text = formattedDateString;

UI Date Picker not Displaying in Label

When I go to a viewcontroller in my app, I have two date pickers. Both of which have labels, and if either one is moved they update accordingly.
The start date is set to automatically be today's date. While it saves to Core Data just fine it doesn't display in the label unless I change it to a different day.
Basically when I load that screen the label should have today's date, and change if I change it in the date picker.
I figure it's something simple overlooking, but I can't figure out what it is, any help would be appreciated.
- (void)viewDidLoad
{
[super viewDidLoad];
svos = self.scrollView.contentOffset;
[self.startDate addTarget:self action:#selector(datePickerChanged:) forControlEvents:UIControlEventValueChanged];
[self.endDate addTarget:self action:#selector(datePickerChanged1:) forControlEvents:UIControlEventValueChanged];
AppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
if(!appDelegate.dateProductionDroneStartDate)
[self.startDate setDate:[NSDate date]];
else
{
[self.startDate setDate:appDelegate.dateProductionDroneStartDate];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM-dd-yyyy "];
NSString *strDate = [dateFormatter stringFromDate:self.startDate.date];
self.productionDroneStartDate.text = strDate;
}
NSDate *now = [NSDate date];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:1];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *newDate2 = [gregorian dateByAddingComponents:components toDate:now options:0];
if(!appDelegate.dateProductionDroneEndDate)
[self.endDate setDate:newDate2];
else
{
[self.endDate setDate:appDelegate.dateProductionDroneEndDate];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM-dd-yyyy "];
NSString *strDate = [dateFormatter stringFromDate:self.endDate.date];
self.productionDroneEndDate.text = strDate;
}
self.txtProductionName.text = appDelegate.strProductionName;
self.txtProductionScene.text = appDelegate.strProductionScene;
self.txtProductionReel.text = appDelegate.strProductionReel;
self.txtProductionName.delegate = self;
self.txtProductionReel.delegate = self;
self.txtProductionScene.delegate = self;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
}
SO far what I got from ur question and code, simple mistake is as below :-
- (void)viewDidLoad
{
[super viewDidLoad];
....
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM-dd-yyyy "];
NSString *strDate = [dateFormatter stringFromDate:self.startDate.date];
AppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
if(!appDelegate.dateProductionDroneStartDate) //As initially this will be true and current date will be set.
[self.startDate setDate:[NSDate date]];
//So as you set the current date in self.startDate but not in label.
//Added below line for setting your label
else
{
[self.startDate setDate:appDelegate.dateProductionDroneStartDate];
}
self.productionDroneStartDate.text = strDate;
......
}
This should get you working further.
Why can't you just set the label with todays date at start without even touching the Picker. Then when the picker is moved as you suggest the label will be updated as expected.

UIDatePicker as inputView

I have two problems at UIDatePicker as inputView.
1: When I select "April 17 9 00 PM" in datePicker,
dateTextField outputs "2014-04-107 21:00:00".
2: UTC of My country is +0900.
But, NSLog outputs "2014-04-17 12:00:00 +0000".
How do I fix these problems?
#property IBOutlet UITextField* dateTextField;
#property IBOutlet UIDatePicker* datePicker;
#property UIToolbar* toolBar;
- (void)viewDidLoad
{
self.datePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 600, 320, 216)];
[self.view addSubview:self.datePicker];
self.toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, -40, 320, 40)];
self.toolBar.barStyle = UIBarStyleBlackOpaque;
UIBarButtonItem* doneButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dateTextFieldShouldReturn)];
NSArray* finishBarItems = [NSArray arrayWithObjects:doneButton, nil];
[self.toolBar setItems:finishBarItems animated:YES];
self.dateTextField = [[UITextField alloc]initWithFrame:CGRectMake(40, 293, 250, 30)];
self.dateTextField.delegate = self;
self.dateTextField.inputView = self.datePicker;
self.dateTextField.inputAccessoryView = self.toolBar;
[myScrollView addSubview:self.dateTextField];
}
- (void)dateTextFieldShouldReturn
{
NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"yyyy-MM-DD HH:mm:ss"];
NSString* dateString = [formatter stringFromDate:self.datePicker.date];
[self.dateTextField setText:dateString];
[self.dateTextField resignFirstResponder];
NSDate* date = [formatter dateFromString:self.dateTextField.text];
NSLog(#"%#", date);
}
Like this works for me:
- (void)dateTextFieldShouldReturn
{
NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_GB"]];
[formatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss zzz"];
NSString* dateString = [formatter stringFromDate:self.datePicker.date];
[self.dateTextField setText:dateString];
[self.dateTextField resignFirstResponder];
NSDate* date = [formatter dateFromString:self.dateTextField.text];
NSLog(#"%#", date);
}

Getting date from the label onto the datepicker

I am working with the UIdatepicker. I am fetching the time from the datepicker and showing the date on the label. Once i select the date it is shown on the label and datepicker is hidden away. Now i want that when i call the picker it should show me the selection bar of the datepicker on the date of the label which i selected previously selected on the datepicker.Code i am using is shown below:-
-(void)datepickershown:(UILabel *)time animated:(BOOL)animated
{
UIActionSheet *actionsheet = [[UIActionSheet alloc] initWithTitle:#"Select the Time" delegate:self cancelButtonTitle:nil destructiveButtonTitle:#"DONE" otherButtonTitles:Nil, nil];
pickdate = [[UIDatePicker alloc] initWithFrame:[actionsheet bounds]];
pickdate.hidden = NO;
//pickdate.date = [NSDate date];
pickdate.datePickerMode = UIDatePickerModeTime;
[pickdate addTarget:self action:#selector(changeDateInLabel:) forControlEvents:UIControlEventValueChanged];
[actionsheet addSubview:pickdate];
[actionsheet showInView:self.view];
[actionsheet setBounds:CGRectMake(0,0,320, 500)];
CGRect pickerRect = pickdate.bounds;
pickerRect.origin.y = -90;
pickdate.bounds = pickerRect;
}
- (IBAction)changeDateInLabel:(id)sender
{
NSLog(#"I Am Changing the values");
df = [[NSDateFormatter alloc] init];
df.timeStyle = NSDateFormatterShortStyle;
}
As you want the datepicker to show the date you have already set on the label.
For this you need to use following method.
- (void)setDate:(NSDate *)date animated:(BOOL)animated
Pass the date whatever you have selected and is shown in the label.
try this
[self.datePicker setDatePickerMode:UIDatePickerModeDate];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd-MM-yyyy"];
if([dateFormat dateFromString:yourDateLabel.text])
{
NSDate *date = [dateFormat dateFromString:yourDateLabel.text];
[self.datePicker setDate:date];
}
hope this will work

Resources