Change the date format of date picker - ios

I would like to change the format of like the image attached
from YYYY/MM/DD to dd/MMM hh:min a but unfortunately i cannot do that
Here is my work in view controller 3
#import "ViewController3.h"
#interface ViewController3 ()
#end
#implementation ViewController3
#synthesize text;
- (void)viewDidLoad {
[super viewDidLoad];
datePicker=[[UIDatePicker alloc]init];
datePicker.datePickerMode=UIDatePickerModeDate;
[self.text setInputView:datePicker];
UIToolbar *toolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
[toolBar setTintColor:[UIColor grayColor]];
UIBarButtonItem *doneBtn=[[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:#selector(ShowSelectedDate)];
UIBarButtonItem *space=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[toolBar setItems:[NSArray arrayWithObjects:space,doneBtn, nil]];
[self.text setInputAccessoryView:toolBar];
}
-(void)ShowSelectedDate
{ NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
[formatter setDateFormat:#"dd/MMM hh:min a"];
self.text.text=[NSString stringWithFormat:#"%#",[formatter stringFromDate:datePicker.date]];
[self.text resignFirstResponder];
}
#end
Do you have any other idea?

You'll want to set up your date formatter in the original date format, and then convert the result to the new date format.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"YYYY/MM/DD"];
NSString *originalDateString = [dateFormatter stringFromDate:datePicker.date];
[dateFormatter setDateFormat:#"dd/MMM hh:mm a"];
NSDate *newDate = [dateFormatter dateFromString:origianalDateString];
self.text.text = [NSString stringWithFormat:#"%#", [dateFormatter stringFromDate:newDate]];

Related

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;

No known class method for selector 'datePicker'

I have searched similar questions here to help me solve mine, all in vain. I want the UItextField when clicked to pull up the UIDatePicker, then the user can select a date, and then set it in the UITextfield. However I keep getting an error for 'datePicker'. not sure what else I am supposed to do.
This is the part of my app that calls the datePicker
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
[pd resignFirstResponder]; //the textField that you will set the selected date
datePicker = [[UIDatePicker alloc] init]; //declared uidatepicker component
pickerViewDate = [[UIActionSheet alloc] initWithTitle:#"Select the date!"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)];
datePicker.datePickerMode = UIDatePickerModeDate; //set your spesific mode
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]]; //or another LocaleIdentifier instead of en_US
[dateFormatter setDateFormat:#"dd.MM.yyyy"]; //desired format
[datePicker addTarget:self action:#selector(dateChanged) forControlEvents:UIControlEventValueChanged]; //the function would be fired when user change the date in datePicker
//now preparing the toolbar which will be displayed at the top of the datePicker
pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerToolbar.barStyle=UIBarStyleBlackOpaque;
[pickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneButtonClicked)]; //barbutton item is "DONE" and doneButtonClicked action will be fired when user clicks the button.
[barItems addObject:flexSpace]; // set the left of the bar
[pickerToolbar setItems:barItems animated:YES];
[pickerViewDate addSubview:pickerToolbar];
[pickerViewDate addSubview:datePicker];
[pickerViewDate showInView:self.view];
[pickerViewDate setBounds:CGRectMake(0,0,320, 464)]; //you can change the position
}
-(IBAction)dateChanged{
NSDateFormatter *FormatDate = [[NSDateFormatter alloc] init];
[FormatDate setLocale: [[NSLocale alloc]
initWithLocaleIdentifier:#"en_US"]];
[FormatDate setDateFormat:#"dd.MM.yyyy"];
pd.text = [FormatDate stringFromDate:[UIDatePicker datePicker]];
}
You're not going to be able to Google the cause of the errors in your code. You will need to figure it out, as your code is unique.
As #madmik3 points out, this line is wrong:
pd.text = [FormatDate stringFromDate:[UIDatePicker datePicker]];
That code is trying to call a UIDatePicker class method "datePicker", which doesn't make any sense.
What you probably want instead is:
pd.text = [FormatDate stringFromDate: [datePicker date]];
That code is asking your date picker object, in the variable datePicker, for it's date property (the current date set in the date picker.

ERROR SHOWING in program code ios

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

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);
}

Custom UIView can't assign values to subviews

I have a UIView subclass and it is connected with a xib. I am not creating views in layoutSubviews method and only in xib. Its confusing when to use self.ivar and _ivar. I tried to set the text for a textField which is a subview of my UIView subclass. Can anyone help me?? I tried to set or get the text to my textfield from my viewController. But I can't succeed. Here is a code.
Here it is my UIView custom class DateRangeView.m
#import "DateRangeView.h"
#implementation DateRangeView
#synthesize tfStartDate = _tfStartDate;
#synthesize tfEndDate = _tfEndDate;
#synthesize startDate = _startDate;
#synthesize endDate = _endDate;
#synthesize dateFormatter = _dateFormatter;
#synthesize datePicker = _datePicker;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"DateRangeView" owner:self options:nil];
[self addSubview:[topLevelObjects objectAtIndex:0]];
[self baseInit];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
// Initialization code
[self baseInit];
}
return self;
}
- (void)baseInit {
NSDateFormatter *df = [[NSDateFormatter alloc]init];
[df setDateStyle:NSDateFormatterMediumStyle];
_dateFormatter = df;
_startDate = [NSDate date];
_endDate = [NSDate date];
}
- (void)awakeFromNib {
[super awakeFromNib];
_datePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 200, 320, 216)];
[_datePicker setDatePickerMode:UIDatePickerModeDate];
[_datePicker setDate:[NSDate date]];
_tfStartDate.inputView = _datePicker;
_tfEndDate.inputView = _datePicker;
UIToolbar *keyboardToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0,self.frame.size.width, 44)];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(cancelButtonPressed:)];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneButtonPressed:)];
[keyboardToolbar setItems:[[NSArray alloc] initWithObjects:cancelBtn, flexibleSpace, doneButton, nil]];
[keyboardToolbar setTranslucent:YES];
[keyboardToolbar setTintColor:[UIColor blackColor]];
[_tfStartDate setInputAccessoryView:keyboardToolbar];
[_tfEndDate setInputAccessoryView:keyboardToolbar];
_tfStartDate.text = [_dateFormatter stringFromDate:_startDate];
_tfEndDate.text = [_dateFormatter stringFromDate:_endDate];
}
- (void)doneButtonPressed:(id)sender {
if ([_tfStartDate isFirstResponder]) {
self.startDate = _datePicker.date;
[_tfStartDate resignFirstResponder];
}else if ([_tfEndDate isFirstResponder]) {
self.endDate = _datePicker.date;
[_tfEndDate resignFirstResponder];
}
}
- (void)cancelButtonPressed:(id)sender {
[_tfStartDate resignFirstResponder];
[_tfEndDate resignFirstResponder];
}
- (void)setStartDate:(NSDate *)startDate {
_startDate = startDate;
_tfStartDate.text = [_dateFormatter stringFromDate:_startDate];
}
- (void)setEndDate:(NSDate *)endDate {
_endDate = endDate;
_tfEndDate.text = [_dateFormatter stringFromDate:_endDate];
}
Here is my code for adding view into viewcontroller and assigning text to textfield and tried to getting the text? But i am not successful in both setting and getting the text.
rangeView = [[DateRangeView alloc]initWithFrame:CGRectMake(0, 0, 320, 150)];
[self.view addSubview:rangeView];
rangeView.startDate = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *startDateComponents = [calendar components:NSDayCalendarUnit fromDate:[NSDate date]];
[startDateComponents setDay:5];
NSDate *beginDate = [calendar dateByAddingComponents:startDateComponents toDate:[NSDate date] options:0];
rangeView.endDate = beginDate;
NSLog(#"start %#", rangeView.tfStartDate.text);
NSLog(#"end %#", rangeView.tfEndDate.text);
Log shows null.. Any help would be appreciated. I have attached a whole code of my UIView only to understand what I am doing.
In your code, I cannot see the initialization for self.tfStartDate and self.tfEndDate.
So, there are 2 possibilities:
they are meant to be IBOutlets: then, check in your nib file that they are also connected to the relevant views;
they are not defined in your xib: in this case just initialize them in viewDidLoad.
As to the use of self.ivar vs. _ivar, the only difference is in the fact that the former will handle reference counting for you; with the latter, you have to do it yourself. So, in this case it is not relevant which one you are using. The issue lies with their initialization so that they reference your subviews correctly.

Resources