简体   繁体   English

关闭时间

[英]Close elapsed time

I use collectionView to display time and price . 我使用collectionView来显示时间价格 Now it looks like this. 现在看起来像这样。
iPhone屏幕快照

Me need close elapsed time. 我需要关闭时间。 For example in screenShot time is 14:51 , me need to close and fill color elapsed time (grey color) the time before 14:00 . 例如在screenShot时间为14:51时 ,我需要关闭填充颜色经过的时间(灰色) 14:00之前的时间。 It's from 1:00 to 13:00 . 1:0013:00

For load price and time I use backend and firestore in func getDataFromBackend() 对于加载价格和时间,我在func getDataFromBackend()使用了后端和func getDataFromBackend()

It's my code: 这是我的代码:

class BookTimeViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

    var priceTime: [PriceTime] = []
    let curTime = Date()

    lazy var hourTimeFormatter: DateFormatter = {

        let formatter = DateFormatter()
        formatter.dateFormat = "HH"
        return formatter

    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        getDataFromBackend()

    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return priceTime.count

    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "timeCell", for: indexPath) as! BookTimeCell

        cell.timeLabel.text = "\(priceTime[indexPath.item].time):00"
        cell.priceLabel.text = "\(priceTime[indexPath.item].price) руб."

        return cell

    }

    func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {

    // I think on this i should close the time 

    }

    func getDataFromBackend() {

        MBProgressHUD.showAdded(to: self.view, animated: true)

        let newDate = dateFormatter.date(from: selectedDate)
        let dateForURL = dateTimeFormatter.string(from: newDate!)

        let url = URL(string: "https:....")

        var request = URLRequest(url: url!)

        request.httpMethod = "GET" 

        let task = URLSession.shared.dataTask(with: request, completionHandler: { (data, response, error) in

            if let err = error {

                print(err)

            } else {

                do {

                    if let jsonResult = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String: Any] {

                        if let prices = jsonResult["prices"] as? [[String: Int]]{

                            for price in prices {

                                self.priceTime.append(PriceTime(price))
                            }
                        }
                    }

                    DispatchQueue.main.async {

                        self.collectionView.reloadData()

                        MBProgressHUD.hide(for: self.view, animated: true)
                    }

                } catch {

                    print("error")
                }
            }
        })

        task.resume()
    }
}

Its suppose your price.time is an Int so that 10 etc. You can't compare time only so append Today's date string in this time. 它假设您的price.time是一个Int所以10等。您不能只比较时间,所以在这个时间后面附加Today的日期字符串。

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy"
let now = Date()
let todayDateStr = dateFormatter.string(from: now)
let priceTime = todayDateStr + " \(19):00"
print(priceTime)

Output: 04-05-2018 19:00 输出:04-05-2018 19:00

Now make a date object from this dateString. 现在从此dateString创建一个日期对象。

dateFormatter.dateFormat = "dd-MM-yyyy HH:mm"
if let priceTimeDateObj = dateFormatter.date(from: priceTime) {
    if priceTimeDateObj < now {
        print("Close this")
    }
}

Or a more simple way you can convert both time into seconds and compare. 或更简单的方法是将时间都转换为秒并进行比较。

dateFormatter.dateFormat = "HH"
let hours = Int(dateFormatter.string(from: now)) ?? 0
dateFormatter.dateFormat = "mm"
let minutes = Int(dateFormatter.string(from: now)) ?? 0

/// This is current time in seconds for today. Example: 18:24 = 66240
let seconds = (hours * 60 * 60) + (minutes * 60) 

let priceTimeSeconds = (priceItem.time * 60 * 60)
if  priceTimeSeconds < seconds {
    print("Close this")
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM