func setTimeLeft() { let timeNow = NSDate() {
if timeEnd.compare(timeNow as Date) == ComparisonResult.orderedDescending {
let calendar = NSCalendar.current
let components = calendar.components([.day , .hour , .minute , .second] , fromDate: timeNow, toDate: timeEnd, options: [])
var dayText = String(components.day) + "d "
var hourText = String(components.hour) + "h "
// Hide day and hour if they are zero
if components.day <= 0 {
dayText = ""
if components.hour <= 0 {
hourText = ""
}
}
timeLeftLabel.text = dayText + hourText + String(components.minute) + "m " + String(components.second) + "s"
} else {
timeLeftLabel.text = "Ended"
}
}
i'm newbie and i just stuck at this error error at line
let components = calendar.components([.day , .hour , .minute , .second] , fromDate: timeNow, toDate: timeEnd, options: [])
Use Calendar
instead of NSCalendar
because NSCalendar.current
will also give Calendar
instance and then get DateComponents
from calendar using dateComponents(_:from:to:)
method.
let calendar = Calendar.current
let components = calendar.dateComponents([.day , .hour , .minute , .second], from: timeNow, to: timeEnd)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.