Swift (or more accurately, the Foundation framework from which Swift gets its date and time classes) gets a bad rap for complex date and time programming, but that’s because date and time programming is complex, and many programmers have erroneous ideas on the topic.
This blog used to be home to a four-part series on date and time programming in Swift. I’ve recently updated that series and moved it to the Auth0 Developer Blog (the one that has much greater reach and also pays my mortgage). It’s also a four-parter:
- Introduction to Date and Time Programming in Swift, Part 1: Learn how to create dates and times using Swift’s powerful date and time objects.
- Introduction to Date and Time Programming in Swift, Part 2: Now that you can create dates and times in Swift, learn how to display date and time values to your users.
- Date and Time Calculations in Swift, Part 1: Learn how to perform date and time calculations with Swift’s powerful date and time objects.
- Date and Time Calculations in Swift, Part 2: Improve your Swift date and time calculations with syntactic magic.
With these articles, you’ll be able to answer questions like:
- What will the day and time be 10,000 hours into 2023?
- What date is the first Friday of 2024?
- What date is the first National Donut Day of 2023?
- What date is the Thursday of the 33rd week of the year?
- What is the actual date of September 50, 2023?
- What day of the week and week of the year will April Fools’ Day 2023 fall on?
- What’s 3:30 p.m. Pacific on the 3rd Thursday of July in the Coptic Calendar system in Melbourne, Australia’s time zone?
- When does a 90-day warranty that starts today expire?
- What is the date of the next Sunday? Or the previous Sunday?
- When is the next Friday the 13th? How many Friday the 13ths will there be in 2024?
- Can you write code like this:
let futureDate = (2.months + 3.days + 4.hours + 5.minutes + 6.seconds).fromNow
I answer all of these questions in this series, so check these articles out!