Ted
Ted

Reputation: 23746

ios date picker display month number instead of name

By default, the date picker shows 1 - January - 2014.

Can user pick as

1 - 1 - 2014 ?

How? Thanks!

Upvotes: 1

Views: 2958

Answers (3)

Anurag Mishra
Anurag Mishra

Reputation: 109

 func datePickerValueChanged(sender:UIDatePicker) {

    let dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "dd-MMM-yyyy"
    let dateString = dateFormatter.stringFromDate(sender.date)
    departureDateTextField.text = dateString

}

Upvotes: 0

Priyanta Singh
Priyanta Singh

Reputation: 77

@Luis is right, if you want the month in numbers you can get. below code will not change how UIDatePicker shows month. but will change your selected month in number. and you can pick as 1 - 1 - 2014.

NSDateFormatter *date = [[NSDateFormatter alloc]init];
    [date setDateFormat:@"dd-M-YYYY"];

    YourDatepicker.datePickerMode = UIDatePickerModeDate;
    NSString *date1 = [date stringFromDate:YourDatepicker.date];

date1 will give you the 1 - 1 - 2014.

Upvotes: 1

Luis Miguel Sierra
Luis Miguel Sierra

Reputation: 734

You can't change the date picker to that format. Apple provides you the following datepicker modes, each of them has its own format:

UIDatePickerModeTime

The date picker displays hours, minutes, and (optionally) an AM/PM designation. The exact items shown and their order depend upon the locale set. An example of this mode is [ 6 | 53 | PM ].

UIDatePickerModeDate

The date picker displays months, days of the month, and years. The exact order of these items depends on the locale setting. An example of this mode is [ November | 15 | 2007 ].

UIDatePickerModeDateAndTime

The date picker displays dates (as unified day of the week, month, and day of the month values) plus hours, minutes, and (optionally) an AM/PM designation. The exact order and format of these items depends on the locale set. An example of this mode is [ Wed Nov 15 | 6 | 53 | PM ].

UIDatePickerModeCountDownTimer

The date picker displays hour and minute values, for example [ 1 | 53 ]. The application must set a timer to fire at the proper interval and set the date picker as the seconds tick down.

If you want any other format, you must implement your own datePicker.

Upvotes: 4

Related Questions