【问题标题】:UIDatePicker issue on 31st December12 月 31 日的 UIDatePicker 问题
【发布时间】:2012-04-13 10:22:53
【问题描述】:

这看起来很奇怪...我在 actionSheet 中使用 UidatePicker。单击按钮时,我会显示操作表。当我选择日期为 2012 年 12 月 31 日并单击完成时,我得到的日期是 2013 年 12 月 31 日。所有年份都在发生这种情况,并且仅在 12 月 31 日发生。我做错了什么吗?这是我的代码

-(void)dateButtonPressed:(id)sender //Method invoking the action sheet on button press
{
actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];

CGRect pickerFrame = CGRectMake(0, 40, 0, 0);

closingdatePicker = [[UIDatePicker alloc] initWithFrame:pickerFrame];
closingdatePicker.tag=10;
closingdatePicker.datePickerMode=UIDatePickerModeDate;


pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerToolbar.barStyle = UIBarStyleBlackTranslucent;
[pickerToolbar sizeToFit];

NSMutableArray *barItems = [[NSMutableArray alloc] init];

UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(DatePickerCancelClick:)];
[barItems addObject:cancelBtn];

UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];

UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(DatePickerDoneClick:)];
[barItems addObject:doneBtn];

[pickerToolbar setItems:barItems animated:YES];

[actionSheet addSubview:pickerToolbar];
[actionSheet addSubview:closingdatePicker];
[actionSheet showInView:self.view];
[closingdatePicker release];
[actionSheet setBounds:CGRectMake(0,0,320, 464)];
[actionSheet release];
}

- (void)DatePickerDoneClick:(id)sender{    //doen click and my date is stored in selectedDate
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MM/dd/YYYY"];

//Gets our picker
UIDatePicker *ourDatePicker = (UIDatePicker *) [actionSheet viewWithTag:10];

NSDate *selectedDate = [ourDatePicker date];

closingDate.text=[NSString stringWithFormat:@"%@",[formatter stringFromDate:selectedDate]];

[actionSheet dismissWithClickedButtonIndex:0 animated:YES];
}

【问题讨论】:

    标签: ios5 nsdate uidatepicker


    【解决方案1】:

    只需使用 yyyy 而不是 YYYY 这在使用 dateformater 时会发生一段时间

    【讨论】:

    • 有点晚了,但你能解释一下为什么它发生在大写字母上吗?感谢您的回答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多