定时器coutdown在两个日期之间迅速(Timer coutdown between two dates in swift)

编程入门 行业动态 更新时间:2024-10-05 03:21:29
定时器coutdown在两个日期之间迅速(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) }

I want to integrate timer between two date one is selected date and another is current date on Button title like any other countdown app.

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") }

I have checked your code and its working on my system.You can run it with some little changes. Below is my code.

@IBOutlet weak var btnTimeLabel : UIButton!

Put this code on 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) }

And I have set next date and current date in your function.

@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") }

更多推荐

本文发布于:2023-08-04 22:05:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1422209.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:定时器   日期   两个   coutdown   swift

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!