来自uidatepicker的奇怪日期

时间:2011-05-30 11:08:33

标签: iphone ipad

我使用日期选择器并将其模式设置为

pickerView.datePickerMode = UIDatePickerModeDate ;

当选择august 3o,2011

显示我使用

NSDate * selected = [pickerView date];
NSString * date = [selected description];
DateLabel.text = date;

输出为2011-08-29 21:00:00:00 + 0000

有关如何仅显示正确日期的任何建议

2 个答案:

答案 0 :(得分:3)

查看NSDateFormatter课程。

尝试使用:

NSDate *selected = [pickerView date];

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeStyle:kCFDateFormatterNoStyle];
[formatter setDateStyle: kCFDateFormatterShortStyle];

NSString *date = [formatter stringFromDate:selected];
[formatter release];

答案 1 :(得分:0)

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"DD/MM/yyyy"];//you can change format here @"MM/yyyy"
DateText.text = [dateFormatter stringFromDate:datePicker.date];
[dateFormatter release];