UIDatePicker minus exact time - Swift

I make an alarm, where he will tell you how many hours and minutes of sleep you will receive. I installed UIDatePicker where the user chooses what time they want to wake up. It also reports the exact time to the very second. The part I'm stuck with is how many hours of sleep they are going to get. I tried just basically subtracting the exact time from the UIDatePicker . It worked if they were both in AM. For example, if a user wanted to wake up at 10:30, and only 9:30, all you have to do is subtract 10:30 from 9:30 to get 1 hour. I soon realized that this would not work if they were different times of the day, for example. AM or PM.

How I got time from UIDatePicker

 func handler(sender: UIDatePicker) { var timeFormatter = NSDateFormatter() timeFormatter.timeStyle = NSDateFormatterStyle.ShortStyle var strDate = timeFormatter.stringFromDate(theDatePicker.date) } theDatePicker.addTarget(self, action: Selector("handler:"), forControlEvents: UIControlEvents.ValueChanged) 

How did I get the exact time

 var date = NSDate() var outputFormat = NSDateFormatter() outputFormat.locale = NSLocale(localeIdentifier:"en_US") outputFormat.dateFormat = "HH:mm:ss" timeLabel.text = (outputFormat.stringFromDate(date)) var timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("getTime"), userInfo: nil, repeats: true) 

My question is:

How to subtract UIDatePicker from the exact time to get the hours of sleep that the user receives?

theff

+5
source share
2 answers

Here is an example from a Swift playground:

 // Setting up a date since I don't have a UIDatePicker let dateString = "2014-11-12 07:25" let dateFormatter: NSDateFormatter = NSDateFormatter() dateFormatter.dateFormat = "yyyy-MM-dd hh:mm" var wakeupTime: NSDate = dateFormatter.dateFromString(dateString)! // var wakeupTime: NSDate = theDatePicker.date let fromDate = NSDate() let gregorianCalendar: NSCalendar = NSCalendar(calendarIdentifier: NSGregorianCalendar)! let flags: NSCalendarUnit = .HourCalendarUnit | .MinuteCalendarUnit let components = gregorianCalendar.components(flags, fromDate: fromDate, toDate: wakeupTime, options: NSCalendarOptions(0)) println("\(components.hour) hours, \(components.minute) minutes") 
+5
source

You can use the NSCalendar components:fromDate:toDate:options: method, for example:

 @IBAction func valueChangedForPicker(sender: UIDatePicker) { let now = NSDate() let wakeUpTime = sender.date let calendar = NSCalendar.currentCalendar() let components = calendar.components(.HourCalendarUnit | .MinuteCalendarUnit | .SecondCalendarUnit, fromDate: now, toDate: wakeUpTime, options: nil) println(String(format: "%02d:%02d:%02d", components.hour, components.minute, components.second)) } 

If you get negative values, this is because fromDate not earlier than toDate . In this case, if you are dealing with NSDatePicker only with the time, you can set the wakeUpTime time to make sure that it is in the future.

 var wakeUpTime = datePicker.date if wakeUpTime.compare(now) == .OrderedAscending { wakeUpTime = calendar.dateByAddingUnit(.DayCalendarUnit, value: 1, toDate: wakeUpTime, options: nil)! } 
+12
source

Source: https://habr.com/ru/post/1206506/


All Articles