Now that we know a bit about Date and DateComponents, let’s actually get a readable date in our Swift app, shall we? This is the job of DateFormatter. DateFormatter is a class that can take a Date, and output a String describing that time/date as its format instructions dictate. In other words, you tell it what you want to know, and it will give you a String that says that. It has a few pretty useful built-in formats, and the capability to accept a custom date format string.
There’s not too much to the actual creation of an DateFormatter object. It pretty much just has an empty initializer (and an NSCoding one, but we aren’t covering NSCoding today). After initializing your DateFormatter, you set the appropriate style for the date and time, and that’s all you need.
[Read more…]