5

I am using a DatePicker with popups when a user click a textField. However, the Picker displays Month first, not date first and it isn't British way to write a date. Is there any way to set the date format in DatePicker to British way such as dd/mm/yyyy? I am using an Achtionsheet:

-(IBAction)acsheet:(id)sender
{

    actionSheet = [[UIActionSheet alloc] initWithTitle:nil 
                                              delegate:nil
                                     cancelButtonTitle:nil
                                destructiveButtonTitle:nil
                                     otherButtonTitles:nil];
    [actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];

    //CGRect pickerFrame = CGRectMake(0, 45, 0, 0);
    pickerView1 = [[UIDatePicker alloc] init];
    pickerView1.datePickerMode = UIDatePickerModeDate;  

    [actionSheet addSubview:pickerView1];

    UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Close"]];
    closeButton.momentary = YES; 
    closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
    closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
    closeButton.tintColor = [UIColor blackColor];
    [closeButton addTarget:self action:@selector(dismissActionSheet:)                    
                      forControlEvents:UIControlEventValueChanged];
    [actionSheet addSubview:closeButton];
    [actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];    
    [actionSheet setBounds:CGRectMake(0, 0, 320, 485)];

    [pickerView1 addTarget:self
                    action:@selector(updateLabel:)
          forControlEvents:UIControlEventValueChanged]; 

    UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self  action:@selector(dismissDatePicker:)] ;
    UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height, 320, 44)];
    toolBar.tag = 11;
    toolBar.barStyle = UIBarStyleBlackTranslucent;

    [toolBar setItems:[NSArray arrayWithObjects:spacer, doneButton, nil]];
    [self.view addSubview:toolBar];

}
X Slash
  • 4,133
  • 4
  • 27
  • 35
user973067
  • 327
  • 2
  • 10
  • 20

3 Answers3

17

The UIDatePicker, by default, uses whatever [NSLocale currentLocale] dictates.

The locale property on UIDatePicker was deprecated in iOS 5, but I believe that you could do the following:

NSCalendar *cal = [NSCalendar currentCalendar];
cal.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"];
myDatePicker.calendar = cal;

This should work. If it doesn't, please file a bug and I'll fix it. :)

Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
  • 2
    Thanks Dave, but this doesn't work for some reason. Anyone else with this dilema? I saw BBog's answer here and it looks pretty discouraging: http://stackoverflow.com/questions/9638698/ios-change-language-for-uidatepicker-by-locale – Stavash Sep 09 '12 at 10:34
7

The UIDatePicker is made to adapt to the user's settings. Changing its format using the locale: method was possible but is now deprecated in iOS 5.0.

You can view your format in the settings of your iPhone :

Settings > General > International > Region Format

If you want a specific format, consider making your own picker but it is very discouraged for a date picker.

2

When writing the month in text the british probably would put the month first (or at least it would be acceptable).

But to answer your question I'm not aware of a way to change the display format of the date picker, but you could make you're own using a UIPickerView with a custom delegate and data source.

wattson12
  • 11,176
  • 2
  • 32
  • 34
  • day first when date is like 20/01/11 but writing January 20 2011 is probably ok (disclaimer: I'm not british, but i live in the UK...) – wattson12 Jan 20 '12 at 11:49
  • Thanks. yes it's day first morst of the time, but month first would be acceptable(It's only confusing when people write in American way like 05/07/2012) Thanks for your suggest anyway. – user973067 Jan 20 '12 at 12:32