0
我正在製作一個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點應該是下午12點,但其採取上午12時上午01時IKE在此
@Trung Anh Pham。感謝您的縮進編輯建議。現在看起來很整齊 – Nithin
不客氣! –