How to change UIDatePicker to a specific time (in code)

吃可爱长大的小学妹 提交于 2019-12-10 01:14:38

问题


I need to change the UIDatePicker to a specific dynamically.

My date picker is set to time mode only. i can set the time with

timePicker.setDate(NSDate(), animated: false)

but i cant figure out how to change it to a different time and not to current time.

So how do i change it?

Thanks


回答1:


You've to change the time, you can do it using NSDateComponents and set the modified date to your DatePicker

var calendar:NSCalendar = NSCalendar.currentCalendar()
let components = calendar.components(NSCalendarUnit.HourCalendarUnit | NSCalendarUnit.MinuteCalendarUnit, fromDate: NSDate())
components.hour = 5
components.minute = 50
datePicker.setDate(calendar.dateFromComponents(components)!, animated: true)



回答2:


Swift 3 and 4:

extension UIDatePicker {

   func setDate(from string: String, format: String, animated: Bool = true) {

      let formater = DateFormatter()

      formater.dateFormat = format

      let date = formater.date(from: string) ?? Date()

      setDate(date, animated: animated)
   }
}

Usage:

datePicker.setDate(from: "1/1/2000 10:10:00", format: "dd/MM/yyyy HH:mm:ss")



回答3:


To set the date pick (time only) to a specific time such as "09:00" I would set the NSDateComponents and picker like this.

let calendar = NSCalendar.currentCalendar()
let components = NSDateComponents()
components.hour = 9
components.minute = 0
timePicker.setDate(calendar.dateFromComponents(components)!, animated: true)



回答4:


You can customize it by formatting your Date and Time this way:

let dateString = "12-11-2015 10:50:00"
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy HH:mm:ss"
let date = dateFormatter.dateFromString(dateString)

timePicker.setDate(date, animated: false)



回答5:


you can use it like this

timePicker.setDate(NSDate(timeInterval: 60, sinceDate: NSDate()), animated: false)

or

timePicker.setDate(NSDate(timeIntervalSinceNow: 60), animated: false)

it would set date with difference of 1 minute from current date.




回答6:


Swift 4:

    let calendar = Calendar.current
    var components = DateComponents()
    components.hour = 5
    components.minute = 50

    alert.setDate(calendar.date(from: components)!, animated: false)



回答7:


If u do not want to type multiple lines of code each time when you want Date or Time from string U can copy below function and use as

let DateVar = DateTimeFrmSrgFnc("31/12/1990",FmtSrg: "dd/MM/yyyy")

timePicker.setDate(DateVar, animated: false)

func DateTimeFrmSrgFnc(DateSrgPsgVar: String, FmtSrg FmtSrgPsgVar: String)-> NSDate
{
    // Format: "dd-MM-yyyy HH:mm:ss"

    let dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = FmtSrgPsgVar
    return dateFormatter.dateFromString(DateSrgPsgVar)!
}


来源:https://stackoverflow.com/questions/28985483/how-to-change-uidatepicker-to-a-specific-time-in-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!