1

I'm developing an app where user should able to choose a time slot. i.e 8:00 AM - 8: 30 AM or 2:00 PM - 2: 30 PM. But I want the user to choose only the time slot based on current time. i.e if its 8PM, he shouldn't able to choose slots till 9PM. I need minimum an hour gap. For now I have just hard coded the time slots as shown below and displaying in a picker view.

var items = ["8.00 AM - 8.30 AM","8.30 AM - 9.00 AM","9.00 AM - 9.30 AM","9.30 AM - 10.00 AM","10.00 AM - 10.30 AM","10.30 AM - 11.00 AM","11.00 AM - 11.30 AM","11.30 AM - 12.00 PM","12.00 PM - 12.30 PM","12.30 PM - 1.00 PM","1.00 PM - 1.30 PM","1.30 PM - 2.00 PM","2.00 PM - 2.30 PM","2.30 PM- 3.00 PM","3.00 PM - 3.30 PM","3.30 PM - 4.00 PM","4.00 PM - 4.30 PM","4.30 PM - 5.00 PM","5.00 PM - 5.30 PM","5.30 PM - 6.00 PM","6.00 PM - 6.30 PM","6.30 PM - 7.00 PM","7.00 PM - 7.30 PM","7.30 PM - 8.00 PM","8.00 PM - 8.30 PM","8.30 PM - 9.00 PM"]


func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {

    if pickerView.tag == 1 {
        return dates[row] as? String
    }else{
        return items[row]
    }

}

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    if pickerView.tag == 1 {
        dateTextField.text = dates[row] as? String
    }else if pickerView.tag == 2{


        timeTextField.text = items[row]
    }

When the user selects the time and if it has past, I want to present an alert or I should disable the time slots which has past. Could anyone please help me to achieve this.

Pruthvi Hariharan
  • 551
  • 1
  • 6
  • 23
  • If you need to get the current time, [this](http://stackoverflow.com/questions/38576463/how-do-i-find-out-whether-time-has-passed-swift) post can help – Lahav Jul 25 '16 at 20:29

1 Answers1

1

First of all you want to create a swift file named Calendar or whatever you like and add the following code :

import UIKit

extension NSDate {
    func hour() -> Int
    {
        //Get Hour
        let calendar = Calendar.current
        let components = calendar.components(.hour, from: self as Date)
        let hour = components.hour

        //Return Hour
        return hour!
    }

    func minute() -> Int
    {
        //Get Minute
        let calendar = Calendar.current
        let components = calendar.components(.minute, from: self as Date)
        let minute = components.minute

        //Return Minute
        return minute!
    }

    func toShortTimeString() -> String
    {
        //Get Short Time String
        let formatter = DateFormatter()
        formatter.timeStyle = .short
        let timeString = formatter.string(from: self as Date)

        //Return Short Time String
        return timeString
    }
}

After that you can write on your ViewController in viewDidLoad() something like that to get the time I have added a Label on my storyboard so I can see the current hour. :

    let currentDate = NSDate()
    timeLabel.text = currentDate.hour().description

The above code is from this post: https://stackoverflow.com/a/29319223/6598643

Above i used Swift 3. On the other post its Swift

---EDIT---

Here is the code for the pickerView:

import UIKit

class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {

    @IBOutlet weak var timeLabel: UILabel!
    @IBOutlet weak var pickerView: UIPickerView!
    var array = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()
        let currentDate = NSDate()
        self.pickerView.dataSource = self;
        self.pickerView.delegate = self;
        var nextHour = 0
        var nextMinute = 0
        var forLoop = 0
        timeLabel.text = "\(currentDate.hour().description) : \(currentDate.minute())"
        if currentDate.minute() > 30 {
            nextHour = currentDate.hour()+2
            nextMinute = 30
        }else{
            nextHour = currentDate.hour()+1
            nextMinute = 0
        }
        if nextMinute == 0 {
            forLoop = (21 - nextHour-1)
            for index in 0 ... forLoop{
                array.append("\(nextHour+index):00")
                array.append("\(nextHour+index):30")
            }
            array.append("21:00")
        }else{
            forLoop = (21 - nextHour - 1)

            for index in 0 ... forLoop{
                array.append("\(nextHour+index):00")
                array.append("\(nextHour+index):30")
            }
            array.append("21:00")
        }
    }

    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return array.count
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return array[row]
    }
}

It calculates and loads on an array the times left from the current time until 21:00 and loads them on pickerView.

Hope I helped. Good luck with your project :)

Community
  • 1
  • 1
pRivaT3 BuG
  • 246
  • 1
  • 3
  • 10
  • Thanks for your reply . So you are telling, I should generate an array based on current time instead of hard coding and then checking whether the time has past? Did I understood correctly? And I want a 30 min interval slot like 8:00 AM - 8:30 AM. How do I get that any idea? And I want the time slot till 9.30PM starting from 8 AM. – Pruthvi Hariharan Jul 25 '16 at 21:25
  • @PruthviHariharan There are several ways to do this. That was my first thought when I read your problem. We can discuss this if u want on a chat cause its a lot.... I have low reputation and I can't create a chat, but u can if u want. – pRivaT3 BuG Jul 25 '16 at 21:30