Make a WWDC CountDown

Here is code to make a simple Swift WWDC countdown:
Code Block swift
import Cocoa
let today = Date()
let startDate = "2020-06-22"
let dateFormatter = DateFormatter()dateFormatter.dateFormat = "yyyy-MM-dd"
let finalStartDate = dateFormatter.date(from: startDate)let components = Set<Calendar.Component>([.day, .hour])
print("WWDC20 Will Come on JUNE 22")
let daysToDc = Calendar.current.dateComponents(components, from: finalStartDate!, to: today)
let printableDays = Int(daysToDc.day!)
let printableHours = Int(daysToDc.hour!)
print(String(printableDays)+" days " + String(printableHours) + " hours till WWDC2020")


Humm, arrives 2 days too late…😉

Seriously, is there a question in your post ?


FYI, your code suffers from a very common bug with fixed-format dates, namely that you combine a fixed date format and the current calendar. To see this, try switching your system to use the Buddhist calendar; you’ll get results that are off by roughly 500 years. See QA1480 NSDateFormatter and Internet Dates for more background on this.

In this case it would be better to skip the date formatter entirely and build your date from its components, using the Gregorian calendar. For example:

Code Block
let dc = DateComponents(year: 2020, month: 6, day: 22)
let date = Calendar(identifier: .gregorian).date(from: dc)!


Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
Nope, Claude31

Make a WWDC CountDown
 
 
Q