时隙不能正常工作

时间:2017-07-15 06:11:43

标签: ios swift

我正在制作一个ios应用程序,其中显示时间段我的时间段就像当前时间将是第一次,之后它将以30 30分钟差异开始。

import UIKit
class ViewController2: UIViewController {

    lazy var timeArray = [Any]()
    let formatter = DateFormatter()


    override func viewDidLoad() {
        super.viewDidLoad()
        timeSlots(startTime: "11:44 AM", endTime: "6:00 PM", leadTime: 60, timeInterval: 30)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    func timeSlots(startTime:String , endTime:String , leadTime:Int, timeInterval:Int){
        formatter.dateFormat = "hh:mm a"

        var startingTime = formatter.date(from: startTime)
        let endingTime = formatter.date(from: endTime)

        var firstTimeSlot = Calendar.current.date(byAdding: .minute , value: leadTime, to: startingTime!)
        timeArray.append(formatter.string(from: firstTimeSlot!).localizedUppercase)


        formatter.dateFormat = "mm"
        let newMin = formatter.string(from: firstTimeSlot!)

        print(formatter.string(from: firstTimeSlot!))

        formatter.dateFormat = "hh"
        let newHour = formatter.string(from: firstTimeSlot!)

        print(formatter.string(from: firstTimeSlot!))


        var i = 0


        if Int(newMin)! < 30 {

            let myNewHour:Int =  Int(newHour)!
            let newTimeIS = "30" + String(myNewHour)
            firstTimeSlot = formatter.date(from: newTimeIS)
        }
        else {

            let myNewHour:Int =  Int(newHour)!
            let newTimeIS = "00" + String(myNewHour)
            firstTimeSlot = formatter.date(from: newTimeIS)

        }

        formatter.dateFormat = "hh:mm a"
        print(formatter.string(from: firstTimeSlot!))

        while startingTime! <= endingTime! {

            guard let firstTime = Calendar.current.date(byAdding: .minute , value: i*timeInterval, to: firstTimeSlot!)  else {return}

            print(formatter.string(from: firstTimeSlot!))
            print(formatter.string(from: firstTime))
            print(formatter.string(from: startingTime!))


            startingTime = firstTime
            timeArray.append(formatter.string(from: firstTime).localizedUppercase)
            i += 1

            if firstTime == endingTime! {
                break
            }
        }
        print(timeArray)
    }
}

如果m设置第一次11:44或11 AM接下来应该是下午12点,但是它在12 AM凌晨1点结束这个

1 个答案:

答案 0 :(得分:0)

希望这个答案能解决你的问题! 请注意,如果确实如此:)

<?php the_post_thumbnail('post-thumbnail', ['class' => 'img-responsive my-other-class']); ?>