-(IBAction)pickerChanged:(id)sender
{
UIDatePicker *picker = (UIDatePicker *)sender;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
//NSDate *dateSelected is declared in .h file
self.dateSelected = [picker date];
if(self.textField.tag == 101)
self.textField.text = [dateFormatter stringFromDate:self.dateSelected];
nslog(@"date = %@",self.textField.text);
nslog(@"dateSelected = %@",self.dateSelected);
}
textfield.text和所选日期应该相同,但是在输出日志中....如果我得到日期= 2-1-2012我得到dateselected = 1-1-2012 ...而且日期是保存为1-1-2012.i我无法弄清楚问题。
答案 0 :(得分:2)
这可能是因为不同的时区。添加这个,它适用于我:
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
答案 1 :(得分:0)
以下代码对我有用,希望它也能为您提供帮助。
-(IBAction)pickerChanged:(id)sender
{
UIDatePicker *picker = (UIDatePicker *)sender;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd/MM/yyyy"];
//NSDate *dateSelected is declared in .h file
if(self.textField.tag == 101){
self.textField.text = [dateFormatter stringFromDate:[picker date]];
}
NSLog(@"date = %@",self.textField.text);
NSLog(@"dateSelected = %@",[picker date]);
}