Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get just the date (no time) from UIDatePicker

I am trying to get just the date from UIDatePicker:

myDatePicker.datePickerMode = UIDatePicker.Mode.date
var selectedDate=myDatePicker.date
println(selectedDate)

However, this prints more than the date (it prints 2015-04-09 21:45:13 +0000). How do I get just the date part (without the time)? I also set the date picker Mode property to Date.

like image 578
Amy Avatar asked Apr 07 '15 22:04

Amy


2 Answers

According to the Apple's documentation datePicker.mode should be date so you can use DateFormatter like so

Swift 4

datePicker.datePickerMode = UIDatePicker.Mode.date
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd MMMM yyyy"
let selectedDate = dateFormatter.string(from: datePicker.date)
print(selectedDate)
like image 158
thinklinux Avatar answered Nov 01 '22 16:11

thinklinux


let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MM/dd/yy"
let dateString = dateFormatter.stringFromDate(myDatePicker.date)

You can print dateString, assign it to a label, whatever. The format will be

04/09/15

like image 43
Shades Avatar answered Nov 01 '22 16:11

Shades