【发布时间】:2020-06-16 15:21:20
【问题描述】:
嘿!
这是我的问题:
所以我有一个日期选择器,主要用于在达到设定时间时发送消息。但是,我希望看到时间用完(类似计时器),直到时间到达。最好采用格式 (hh:mm:ss),因为日期选择器设置为计时器格式。
我只是编程的初学者。所以我不知道该怎么办:/
如果有人可以帮助我,我会很高兴!
非常感谢。
let date = NSDate()
let calendar = Calendar.current
let components = calendar.dateComponents([.hour, .minute, .month, .year, .day], from:
date as Date)
let currentDate = calendar.date(from: components)
let userCalendar = Calendar.current
let competitionDate = self.datepicker.isSelected
let competition = userCalendar.date(from: competitionDate as DateComponents)!
let CompetitionDifference = calendar.dateComponents([.hour, .minute, .second], from:
currentDate!, to: competition)
let hoursLeft = CompetitionDifference.hour
let minutesLeft = CompetitionDifference.minute
let secondsLeft = CompetitionDifference.second
print("hours:", hoursLeft ?? "N/A", "minutes:", minutesLeft ?? "N/A", "seconds:",
secondsLeft ?? "N/A")
countDownLabel.text = "\(daysLeft ?? 0) hours, \(hoursLeft ?? 0) minutes, \(minutesLeft
?? 0) seconds"
【问题讨论】:
标签: swift xcode timer datepicker countdown