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);
}
Related
I want to connect and pass the action from ViewController to NSObject class file. In my viewcontroller, there is UISwitch Button and if I switch I want to work it in NSObject class file.
When switch is ON from viewcontroller (UIView) then the action have to work in NSObject class file.
I'm coding with Objective-C.
Here is my ViewController.m and I did this in only ViewController file. Not connected to NSOBject class file.
- (void)viewDidLoad{
[super viewDidLoad];
//UISwitch work at viewDidload
[self.mySwitch addTarget:self
action:#selector(stateChanged:) forControlEvents:UIControlEventValueChanged]; }
- (void)stateChanged:(UISwitch *)switchState{
NSLog(#"current calendar: %#", [[NSCalendar currentCalendar] calendarIdentifier]);
//get the date today
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setCalendar:[NSCalendar currentCalendar]];
NSString *dateToday = [formatter stringFromDate:[NSDate date]];
NSLog(#"Today Date is: %#", [NSDate date]);
self.myLabel.text=dateToday;
if ([switchState isOn]) {
//[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US#calendar=japanese"]];
[formatter setCalendar:[[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierJapanese]];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
[formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSString *dateToday = [formatter stringFromDate:[NSDate date]];
UILabel *dtDate = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320.0f, 20.0f)];
[dtDate setText:dateToday];
self.myLabel.text =dateToday;
NSString *locale = [[NSLocale currentLocale] localeIdentifier];
NSLog(#"current date is: %#", dateToday);
NSLog(#"current locale: %#", locale);
} else {
//[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]];
[formatter setCalendar:[NSCalendar autoupdatingCurrentCalendar]];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
[formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSString *dateToday = [formatter stringFromDate:[NSDate date]];
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320.0f, 20.0f)];
[myLabel setText:dateToday];
self.myLabel.text =dateToday;
// NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
// [formatter setDateStyle:NSDateFormatterMediumStyle];
NSString *locale = [[NSLocale currentLocale] localeIdentifier];
NSLog(#"current locale: %#", locale);
NSLog(#"%#", [formatter stringFromDate: [NSDate date]]);
}
}
If you need just an action then use class methods else create a global variable or a notification observer in NSObject extended class when you get call back from the switch.
- (void)callback:(id)sender
{
[MyObject switchState:sender.state];
}
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;
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]];
-(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);
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];
}