Let's create a function that will return a string telling us how long there is until Christmas that we can then print:
- Define the function:
func howLongUntilChristmas() -> String {
}
- Within the function, get the current calendar and time zone:
let calendar = Calendar.current
let timeZone = TimeZone.current
- Get the current date and time and use the calendar to get the current year:
let now = Date()
let yearOfNextChristmas = calendar.component(.year, from: now)
- Define date components that correspond to midnight on Christmas Day:
var components = DateComponents(calendar: calendar,
timeZone: timeZone,
year: yearOfNextChristmas,
month: 12,
day: 25,
hour: 0,
minute: 0,
second: 0)
- Get a Date object from those components:
var christmas = components...