我怎样才能知道时间是否过去了?迅速

时间:2022-06-21 07:20:30

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.

我正在开发一个应用程序,用户应该可以选择一个时间段。即上午8:00 - 上午8:30或下午2:00 - 下午2:30。但我希望用户只根据当前时间选择时隙。即如果是晚上8点,他不应该选择插槽直到晚上9点。我需要至少一个小时的差距。现在我刚刚对时隙进行了硬编码,如下所示,并在选择器视图中显示。

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.

当用户选择时间并且它已经过去时,我想要发出警报,或者我应该禁用已经过去的时间段。谁能帮助我实现这个目标。

1 个解决方案

#1


1  

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

首先,您要创建名为Calendar或任何您喜欢的swift文件,并添加以下代码:

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. :

之后你可以在viewDidLoad()中的ViewController上写下这样的东西来获得我在故事板上添加Label的时间,这样我就能看到当前的小时。 :

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

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

上面的代码来自这篇文章:https://*.com/a/29319223/6598643

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

上面我使用了Swift 3.另一篇文章是Swift

---EDIT---

Here is the code for the pickerView:

以下是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.

它计算并在数组上加载从当前时间到21:00的剩余时间,并将它们加载到pickerView上。

Hope I helped. Good luck with your project :)

希望我帮忙。祝你的项目好运:)

#1


1  

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

首先,您要创建名为Calendar或任何您喜欢的swift文件,并添加以下代码:

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. :

之后你可以在viewDidLoad()中的ViewController上写下这样的东西来获得我在故事板上添加Label的时间,这样我就能看到当前的小时。 :

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

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

上面的代码来自这篇文章:https://*.com/a/29319223/6598643

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

上面我使用了Swift 3.另一篇文章是Swift

---EDIT---

Here is the code for the pickerView:

以下是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.

它计算并在数组上加载从当前时间到21:00的剩余时间,并将它们加载到pickerView上。

Hope I helped. Good luck with your project :)

希望我帮忙。祝你的项目好运:)