如何格式化UIDatepicker日期?

时间:2011-10-15 09:30:57

标签: iphone objective-c ios uidatepicker

我需要知道如何获得日期d-m-y,就像这个例子4-5-2011一样,特别是没有分钟,小时和+0000

代码:

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

UIDatePicker *pickerVieww = [[UIDatePicker alloc] initWithFrame:pickerFrame];

    pickerVieww.timeZone = [NSTimeZone defaultTimeZone];
    pickerVieww.datePickerMode= UIDatePickerModeDate; 
    //datePicker.maximumDate=[NSDate date]; 

    [pickerVieww addTarget:self 
                    action:@selector(pickerChanged:)  
          forControlEvents:UIControlEventValueChanged];

    pickerVieww.minimumDate =  [NSDate date];
    [pickeView release];

我从pickerChanged 2012-11-14 22:00:00 +0000

获得了这个日期

任何想法都要提前感谢

2 个答案:

答案 0 :(得分:3)

你做了一次datePickingDone

NSDateFormatter *dateformat=[[NSDateFormatter alloc]init];
        [dateformat setDateFormat:@"dd/MM/YY"];
        NSString *formattedDate=[dateformat stringFromDate:urReceivedDate];
    [dateformat release];

答案 1 :(得分:1)

您也可以使用 + (NSString *)localizedStringFromDate:(NSDate *)date dateStyle:(NSDateFormatterStyle)dateStyle timeStyle:(NSDateFormatterStyle)timeStyle

 NSString *dateString = [NSDateFormatter localizedStringFromDate:date 
                                   dateStyle:NSDateFormatterMediumStyle 
                                   timeStyle:NSDateFormatterNoStyle];

关于这一点的好处是,它为您处理本地化(当然它很好而且很短)。检查不同的NSDateFormatterStyles,找到你需要的那个。