日期选择器的单独值,例如日,月和年

时间:2018-07-31 15:00:25

标签: ios swift datepicker

有人可以帮助我分隔datePicker值,例如日,月和年吗?

我想将它们分开进行计算。我不能那样做,我看了较旧的问题,但没有将它们转换为我的问题。

@objc func whenDoneButtonPressed(){

    let dateFormatter = DateFormatter()
    dateFormatter.dateStyle = .long
    dateFormatter.timeStyle = .none
    sulustarihiTextField.text = dateFormatter.string(from: datePicker.date)

    print((sulustarihiTextField.text!))

    self.view.endEditing(true)

}

1 个答案:

答案 0 :(得分:0)

try this

func dateChanged(_ sender: UIDatePicker) {
    let components = Calendar.current.dateComponents([.year, .month, .day], from: sender.date)
    if let day = components.day, let month = components.month, let year = components.year {
        print("\(day) \(month) \(year)")
    }
}