wajeeh wajeeh - 5 months ago 27
iOS Question

how to work with UIDatePicker in Swift?

I built a UI that has 3

UIDatePicker


The first
UIDatePicker
named
date
will be for
Date
only (Mode
.Date
)

Other
UIDatePicker
s names
time_from
and
time_to
will be for
Time
(From time, To time) (Mode
.Time
)

Then I add a
Button
,
When I click the button I need to get two variables:
From_DateTime
and
To_DateTime


(I need the Date from
date
UI and the time from
time_from
,
time_to

enter image description here

enter image description here

I try a lot but without the desired result:

let date = date.calendar
let date_components = date.components(.Calendar, fromDate: date.date)

let date_from_components = dateFrom.calendar.components(.Calendar, fromDate: dateFrom.date)
let date_to_components = dateTo.calendar.components(.Calendar, fromDate: dateTo.date)


let n = date.calendar.dateBySettingHour(date_from_components.hour, minute: date_from_components.minute, second: 5, ofDate: dateFrom.date, options: NSCalendarOptions.MatchStrictly)


This is an example of fails trying.

I need a simple way to get
From_DateTime
and
To_DateTime

Thanks

Answer

Finally I found a way:

let dateFormatter = NSDateFormatter()
    dateFormatter.locale = NSLocale(localeIdentifier: "US")
    dateFormatter.dateFormat = "yyyy-MM-dd"

    let timeFormatter = NSDateFormatter()
    timeFormatter.locale = NSLocale(localeIdentifier: "US")
    timeFormatter.dateFormat = " HH:mm"

    let date_from = dateFormatter.stringFromDate(date.date) + timeFormatter.stringFromDate(dateFrom.date)
    let date_to = dateFormatter.stringFromDate(date.date) + timeFormatter.stringFromDate(dateTo.date)

But still sure that there is another simple way.

Comments