2017-07-15 89 views
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在此

回答

0

希望這個答案解決您的問題! 如果是的話,請點贊:)

import UIKit 

class ViewController: UIViewController { 

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

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
     timeSlots(startTime: "11:44 AM", endTime: "6:00 PM", timeInterval: 30) 
    } 

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

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

     if endingTime! <= startingTime! { 
      return 
     } 

     formatter.dateFormat = "mm" 
     var min = formatter.string(from: startingTime!) 
     formatter.dateFormat = "hh" 
     var hour = formatter.string(from: startingTime!) 

     if Int(min)! <= 30 { 
      min = "30" 
     } else { 
      min = "00" 
      hour = String(Int(hour)! + 1) 
     } 

     formatter.dateFormat = "HH:mm" 
     var tempStartTime = formatter.date(from: "\(hour):\(min)") 
     let endTimeString = formatter.string(from: endingTime!) 
     endingTime = formatter.date(from: endTimeString) 

     formatter.dateFormat = "hh:mm a" 

     timeArray.append(formatter.string(from: tempStartTime!).localizedUppercase) 

     while tempStartTime! < endingTime! { 
      guard let timeSlot = Calendar.current.date(byAdding: .minute , value: timeInterval, to: tempStartTime!) else {return} 

      //print("timeSlot: \(formatter.string(from: timeSlot))") 

      tempStartTime = timeSlot 
      timeArray.append(formatter.string(from: timeSlot).localizedUppercase) 
     } 

     print(timeArray) 
    } 
+0

@Trung Anh Pham。感謝您的縮進編輯建議。現在看起來很整齊 – Nithin

+0

不客氣! –