Having users enter dates is as easy as binding an
@State property of type
Date to a
DatePicker SwiftUI control, but things get a little woolier afterwards.
You see, working with dates is hard. Like, really hard. Way harder than you think. Way harder than I think, and I’ve been working with dates for years.
Take a look at this trivial example:
let now = Date() let tomorrow = Date().addingTimeInterval(86400) let range = now ... tomorrow
That creates a range from now (
Date() is the current date) to the same time tomorrow (86400 is the number of seconds in a day).
That might seem easy enough, but do all days have 86,400 seconds? If they did, a lot of people would be out of jobs! Think about daylight savings time: sometimes clocks go forward (losing an hour) and sometimes go backwards (gaining an hour), meaning that we might have 23 or 25 hours in those days. Then there are leap seconds: times that get added to the clocks in order to adjust for the Earth’s slowing rotation.
If you think that’s hard, try running this from your Mac’s terminal:
cal. This prints a simple calendar for the current month, showing you the days of the week. Now try running
cal 9 1752, which shows you the calendar for September 1752 – you’ll notice 12 whole days are missing, thanks to the calendar moving from Julian to Gregorian.
Now, the reason I’m saying all this isn’t to scare you off – dates are inevitable in our programs, after all. Instead, I want you to understand that for anything significant – any usage of dates that actually matters in our code – we should rely on Apple’s frameworks for calculations and formatting.
In the project we’re making we’ll be using dates in three ways:
We could, if we wanted, do all that by hand, but then you’re into the realm of daylight savings, leap seconds, and Gregorian calendars.
Much better is to have iOS do all that hard work for us: it’s much less work, and it’s guaranteed to be correct regardless of the user’s region settings.
Let’s tackle each of those individually, starting with choosing a sensible wake up time.
As you’ve seen, Swift gives us
Date for working with dates, and that encapsulates the year, month, date, hour, minute, second, timezone, and more. However, we don’t want to think about most of that – we want to say “give me an 8am wake up time, regardless of what day it is today.”
Swift has a slightly different type for that purpose, called
DateComponents, which lets us read or write specific parts of a date rather than the whole thing.
So, if we wanted a date that represented 8am today, we could write code like this:
var components = DateComponents() components.hour = 8 components.minute = 0 let date = Calendar.current.date(from: components)
Now, because of difficulties around date validation, that
date(from:) method actually returns an optional date, so it’s a good idea to use nil coalescing to say “if that fails, just give me back the current date”, like this:
let date = Calendar.current.date(from: components) ?? Date()
The second challenge is how we could read the hour they want to wake up. Remember,
DatePicker is bound to a
Date giving us lots of information, so we need to find a way to pull out just the hour and minute components.
DateComponents comes to the rescue: we can ask iOS to provide specific components from a date, then read those back out. One hiccup is that there’s a disconnect between the values we request and the values we get thanks to the way
DateComponents works: we can ask for the hour and minute, but we’ll be handed back a
DateComponents instance with optional values for all its properties. Yes, we know hour and minute will be there because those are the ones we asked for, but we still need to unwrap the optionals or provide default values.
So, we might write code like this:
let components = Calendar.current.dateComponents([.hour, .minute], from: someDate) let hour = components.hour ?? 0 let minute = components.minute ?? 0
The last challenge is how we can format dates and times, and once again Swift gives us a specific type to do most of the work for us. This time it’s called
DateFormatter, and it lets us convert a date into a string in a variety of ways.
For example, if we just wanted the time from a date we would write this:
let formatter = DateFormatter() formatter.timeStyle = .short let dateString = formatter.string(from: Date())
We could also set
.dateStyle to get date values, and even pass in an entirely custom format using
dateFormat, but that’s way out of the remit of this project!
The point is that dates are hard, but Apple has provided us with stacks of helpers to make them less hard. If you learn to use them well you’ll write less code, and write better code too!
SPONSORED From January 26th to 31st you can join a FREE crash course for iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a senior developer!
Link copied to your pasteboard.