我如何知道时间是否过去了?迅速

时间:2016-07-25 20:04:04

标签: ios swift nsdate

我正在开发一个应用程序,用户应该可以选择一个时间段。即上午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]
    }

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

1 个答案:

答案 0 :(得分:1)

首先,您要创建名为日历的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
    }
}

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

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

以上代码来自这篇文章:https://stackoverflow.com/a/29319223/6598643

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

<强> --- --- EDIT

以下是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]
    }
}

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

希望我能帮到你。祝你的项目顺利:))