ios - 如何在 Swift 中使用 UIDatePicker?

标签 ios iphone swift nsdate uidatepicker

我构建了一个包含 3 个 UIDatePicker

的 UI

第一个名为 dateUIDatePicker 将仅用于 Date(模式 .Date)

其他 UIDatePicker 的名称 time_fromtime_to 将用于 Time (From time, To time) (模式 .Time)

然后我添加一个Button, 当我单击按钮时,我需要获取两个变量: From_DateTimeTo_DateTime

(我需要 date UI 的日期和 time_fromtime_to 的时间 enter image description here

enter image description here

我尝试了很多但没有得到想要的结果:

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)

这是一个尝试失败的例子。

我需要一种简单的方法来获取 From_DateTimeTo_DateTime 谢谢

最佳答案

终于找到办法了:

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)

我相信一定有另一种简单的方法。

关于ios - 如何在 Swift 中使用 UIDatePicker?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35250735/

相关文章:

c# - C# 中惯用的可选返回值

ios - 如何使用 UIPanGestureRecognizer 从 y 值获取 0 到 1 之间的浮点值

iphone - Apple Developer 帐户和配置文件

ios - 如何更改coredata中的记录顺序?

iphone - 如何在未连接到计算机时配置设备

iphone - 如何将视频添加到 iPhone 应用程序

ios - 从 iOS 中的所有 3 个麦克风录音

ios - 检索缩放后的 UIView 的正确位置

iphone - 如何将 BOOL 写入 plist?

xcode - 游戏运行但没有任何反应