簡體   English   中英

swift中兩個日期之間的計時器倒計時

[英]Timer coutdown between two dates in swift

我想在兩個日期之間集成計時器,一個是選擇日期,另一個是按鈕標題上的當前日期,就像任何其他倒計時應用程序一樣。

func countDownDate() {
    var calendar = Calendar.current
    let diffDateComponents = calendar.dateComponents([.day, .hour, .minute, .second], from: self.startDate, to: self.currentDate as Date)
    let countdown = "Days \(String(describing:diffDateComponents.day!)),  Hours: \(String(describing: diffDateComponents.hour!)), Minutes: \(String(describing: diffDateComponents.minute!)), Seconds: \(String(describing: diffDateComponents.second!))"
    print("countdown",countdown)
    var dayText = String(describing: diffDateComponents.day!) + "d "
    var hourText = String(describing: diffDateComponents.hour!) + "h "
    self.button_CreateBid.setTitle(dayText + hourText + String(describing: diffDateComponents.minute!) + "m " + String(describing: diffDateComponents.second!) + "s", for: .normal)
}


let when = DispatchTime.now() + 0.1 // change 2 to desired number of seconds
DispatchQueue.main.asyncAfter(deadline: when) {
    Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(ServiceDetailViewController.countDownDate), userInfo: nil, repeats: true)
}

我已經檢查了您的代碼及其在我的系統上的運行情況。您只需稍作改動就可以運行它。 下面是我的代碼。

@IBOutlet weak var btnTimeLabel : UIButton!

將此代碼放在viewDidLoad

let when = DispatchTime.now() + 0.1 // change 2 to desired number of seconds
        DispatchQueue.main.asyncAfter(deadline: when) {
            Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(ViewController.countDownDate), userInfo: nil, repeats: true)
        }

我已經在你的函數中設置了下一個日期和當前日期。

 @objc func countDownDate() {

        let dateFormatter = DateFormatter()
               dateFormatter.dateFormat = "dd-mm-yyyy" //Your date format
               dateFormatter.timeZone = TimeZone(abbreviation: "GMT+0:00") //Current time zone
               let futuredate = dateFormatter.date(from: "22-02-2018") //according to date format your date string
        var calendar = Calendar.current
        let diffDateComponents = calendar.dateComponents([.day, .hour, .minute, .second], from: futuredate!, to: Date())
        let countdown = "Days \(String(describing:diffDateComponents.day!)),  Hours: \(String(describing: diffDateComponents.hour!)), Minutes: \(String(describing: diffDateComponents.minute!)), Seconds: \(String(describing: diffDateComponents.second!))"
        print("countdown",countdown)
        var dayText = String(describing: diffDateComponents.day!) + "d "
        var hourText = String(describing: diffDateComponents.hour!) + "h "
        btnTimeLabel.setTitle(dayText + hourText + String(describing: diffDateComponents.minute!) + "m " + String(describing: diffDateComponents.second!) + "s", for: .normal)
        //print(dayText + hourText + String(describing: diffDateComponents.minute!) + "m " + String(describing: diffDateComponents.second!) + "s")
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM