Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - Custom UIDatePicker format

i'd like to change the format of my UIDatePicker so it can like that "Year Month Day Hour(24) Min"

Is there any way to do that ???

Thanks

like image 595
Fabrice Froehly Avatar asked Dec 25 '22 07:12

Fabrice Froehly


2 Answers

You Can't Customize your UIDatePicker, Because UIDatePickerMode have four Types of Mode.

public enum UIDatePickerMode : Int {
    case Time // Displays hour, minute, and optionally AM/PM designation depending on the locale setting (e.g. 6 | 53 | PM)
    case Date // Displays month, day, and year depending on the locale setting (e.g. November | 15 | 2007)
    case DateAndTime // Displays date, hour, minute, and optionally AM/PM designation depending on the locale setting (e.g. Wed Nov 15 | 6 | 53 | PM)
    case CountDownTimer // Displays hour and minute (e.g. 1 | 53)
}

You can display only one of the mode as above.

like image 134
Kirit Modi Avatar answered Jan 30 '23 16:01

Kirit Modi


Here is the code to get the date in your desired format:

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "YYYY-MM-dd HH:mm"
let strDate = dateFormatter.stringFromDate(NSDate()) // You can pass your date here as a parameter to get in a desired format
print(strDate)
like image 21
Amrit Sidhu Avatar answered Jan 30 '23 17:01

Amrit Sidhu