I am trying to implement a countdown to a specific date in swift getting days, hours, minutes, and seconds, but for some reason my implementation is returning the wrong amount of days until the event, but is returning the correct hours, minutes, and seconds. I'm not sure what's going wrong here:
let date = Date()
let calendar = Calendar.current
let components = calendar.dateComponents([.second, .minute, .hour, .day], from: date as Date)
let currentDate = calendar.date(from: components)
let userCalendar = Calendar.current
let openingNight = NSDateComponents()
openingNight.year = 2020
openingNight.month = 9
openingNight.day = 10
openingNight.hour = 20
openingNight.minute = 30
openingNight.second = 0
let opener = userCalendar.date(from: openingNight as DateComponents)
let difference = calendar.dateComponents([.second, .minute, .hour, .day], from: currentDate!, to: opener!)
let daysLeft = difference.day
let hoursLeft = difference.hour
let minutesLeft = difference.minute
let secondsLeft = difference.second
daysLabel.text = "\(daysLeft!)"
hourLabel.text = "\(hoursLeft!)"
minuteLabel.text = "\(minutesLeft!)"
secondLabel.text = "\(secondsLeft!)"
it is returning 737661 days, 4 hours, 2 minutes, 28 seconds
currentDateis "Jan 19, 1 at 5:36 PM". That's how you get so many days. - koen