ios - 从日期选择器获取日期到变量中的字符串

标签 ios swift datetime uidatepicker

我有制作和订购应用程序 我有一个 outlet 名为:selectedDate 我想将选定的日期检索到一个变量。我应该在 View 中放置什么确实加载了。

@IBOutlet weak var selectedDate: UIDatePicker!

func datePickerChanged(selectedDate:UIDatePicker) {
    var dateFormatter = DateFormatter()
    dateFormatter.dateStyle = DateFormatter.Style.FullStyle
    dateFormatter.timeStyle = DateFormatter.Style.FulltStyle
    var strDate = dateFormatter.string(from: selectedDate.date)  
} 

最佳答案

class ViewController: UIViewController {

@IBOutlet weak var datePicker: UIDatePicker!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
 @IBAction func datePickerChanged(sender: UIDatePicker) {

    print("print \(sender.date)")

    let dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "MMM dd, YYYY"
    let somedateString = dateFormatter.stringFromDate(sender.date)

    print(somedateString)  // "somedateString" is your string date
}


}

您必须为 datePicker 创建一个 IBAction 并将其命名为“datePickerChanged”,就像创建对象的导出一样。将对象拖动到 Viewcontroller 时选择连接类型“Action”。

关于ios - 从日期选择器获取日期到变量中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41674497/

相关文章:

swift - 生成对象时的 EXC_BAD_INSTRUCTION

ios - swift : boundingRectWithSize return wrong height for a multiline label

ios - 在模拟器上运行,同时拥有不在模拟器上显示类的 SDK(阻止我运行)

javascript - 当日期为 MM/DD/YYYY HH :mm:ss 时,moment.js 返回意外的 fromNow 日期

java - 几个 JDK TimeZone 函数的 joda-time 等价物是什么

objective-c - 自动释放 NSMutableArray 的问题

ios - 在不链接的情况下将 OpenCV 框架嵌入到另一个 xCode 项目中

ios - 多个UIManagedDocument用于读写

ios - -[AXSpeechAction 保留] : message sent to deallocated instance 0x1c37e2b0

javascript - 如何让 moment.js 显示以秒为单位的相对时间?