0
我正在做一个秒表,并需要显示一个计时器,当一个事件正在进行,直到它结束。承认事件开始于晚上8点,当前日期为晚上9点30分,显示01:30:00的秒表将持续滴答,直到end_time到达。这里是我的代码,我需要一种方法来给currentTime的currentDate-的价值START_TIME做日期之间的操作数
var currentTime = 0
func printTime() {
let formatter = DateFormatter()
formatter.dateFormat = "dd/MM/yyyy"
let start_time = self.eventArray[self.clubId!]?.start_time
let end_time = self.eventArray[self.clubId!]?.end_time
self.currentTime = currentDate - start_time
if(currentDate?.compare(start_time!) == .orderedAscending && currentDate?.compare(end_time!) == .orderedAscending)
{
if(timer != nil)
{
timer?.invalidate()
timer = nil
}else
{
timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true, block: { _ in
self.currentTime += 1
let hoursPortion = String(format: "%02d", self.currentTime/60)
let minutesPortion = String(format: "%02d", self.currentTime/60)
let secondsPortion = String(format: "%02d", self.currentTime % 60)
self.sinceBandeauLabel.text = "\(hoursPortion):\(minutesPortion):\(secondsPortion)"
})
}
}