Swift UIDatePicker获取时间
Swift 4 ,取时间有问题,改用下面方法
let date = birthday.date; let dateFormatter = DateFormatter() dateFormatter.dateFormat = "yyyy年MM月dd日" let age = dateFormatter.string(from: date)
Swift 4 ,取时间有问题,改用下面方法
let date = birthday.date; let dateFormatter = DateFormatter() dateFormatter.dateFormat = "yyyy年MM月dd日" let age = dateFormatter.string(from: date)
2017-11-03
//从控件获取Date
let selectDate = birthday.date
//创建Calendar对象calendar(这个对象用于将Date转为DateComponents,DateComponents可以获取Date中的各个组成元素(包括年、月、日、时、分、秒等))
let calendar = Calendar.current
//使用calendar对象的dateComponents方法将selectDate转为DateComponents对象selectDateComponents
let selectDateComponents = calendar.dateComponents([Calendar.Component.year], from: selectDate)
//selectDateComponents点year获取年,类型Int
let selectYear = selectDateComponents.year!
举报