简体   繁体   中英

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 在此处输入图片说明

在此处输入图片说明

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

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)

I am sure there must be another simple way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM