18

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

ilan
  • 4,402
  • 6
  • 40
  • 76

7 Answers7

20

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)
arthankamal
  • 6,341
  • 4
  • 36
  • 51
  • 1
    Please don't ever do this if the `UIDatePicker` is in time only mode. The reason is that, imagine the above code is executed in New York at 14 March 2021. If the code trying to change the hour = 2. It will be very wrong. Due to Day Light Saving, there is no 2am in 14 March 2021. A correct way is to hard coded a timezone without DST like GMT, so that user can read/ write time only information to DatePicker without the above mentioned issue. – Cheok Yan Cheng Nov 10 '21 at 10:45
9

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")
Naveed Ahmad
  • 6,627
  • 2
  • 58
  • 83
zombie
  • 5,069
  • 3
  • 25
  • 54
6

Swift 5

let calendar = Calendar.current
var components = DateComponents()
components.hour = 5
components.minute = 50
    
timePicker.setDate(calendar.date(from: components)!, animated: false)
Michal Šrůtek
  • 1,647
  • 16
  • 17
Jay Mutzafi
  • 161
  • 2
  • 7
5

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)
Tunaki
  • 132,869
  • 46
  • 340
  • 423
Kevin Horgan
  • 1,590
  • 16
  • 20
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)
rbrundritt
  • 16,570
  • 2
  • 21
  • 46
Diamond King
  • 676
  • 10
  • 17
3

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.

Dheeraj Singh
  • 5,143
  • 25
  • 33
  • I need to change to a totally different time. i get a time from server in "HH:mm" format and i need to show it in date picker – ilan Mar 11 '15 at 11:38
0

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)!
}
Sujay U N
  • 4,974
  • 11
  • 52
  • 88