Swift Components Tour – NSDateFormatters

In this lesson

Back in the Swift Components Tour app, we are talking about how to work with dates. Your choices are NSDates, NSDateFormatters, and NSCalendars (or data, presentation, and interaction). Today, it’s NSDateFormatters (or presentation)!

Kyle Roberts
Swift Guru at Large

Kyle's Series

Transcript

Tap on time to skip ahead

00:10

Hello world! Kyle here with Brax.tv and today we’re going to talk about NSDateFormatters.

00:17

To use a date formatter in a basic way, there’s nothing complicated about it, I’m doing it here in only two lines. I guess three if you count the property declaration and the NSDateFormatter. But that’s about as simple as it gets. In viewDidLoad of the UI Stepper view controller, I’m setting the date style property of the date formatter to it’s enum value FullStyle. And I’m setting the date labels text to the current date using the date formatter.

00:53

You can see that we are using this method stringFromDate. And that is a method on NSDateFormatter that will take an NSDate object and turn it into a string. Depending on some of the properties set on the date formatter, and we did set this date style property, then the date formatter is going to format in that NSDate into a readable style. In this case FullStyle. And what that looks like in the app is the day of the week, the full month, the day and then the year. So I’m going to do some setup in this dates playground over here. I’ll be back in just a second.

01:37

Ok we’re back and we’re in the Dates.playground. Up at the top we have the code that we used from the last video talking about NSDates. We have the code that we wrote from the NSDates video. Below this I’ve pretty much copied and pasted what we had in the steppers view controller. We are creating a new NSDateFormatter. Setting the date style to FullStyle. Using that method on the date formatter stringFromDate on the date object from the NSDate code up here. We can see that it is printing out the exact same date in the same exact format and style that the Swift component stores right here.

02:23

What if we wanted to change the style? There’s a bunch of options. Let’s actually use ShortStyle. We can see that it reformats that date to be numbers and slashes. It gets the same point across, we don’t know the day of the week but you can figure that out on your own. But just to show you that there are different styles.

02:48

We know that NSDates don’t just store the day and the month and the year. They don’t actually store any of those, as we’ve talked about. But since they store a value in seconds from January 1, 2001 in UTC time, we should be able to get the time of day from that NSDate using the date formatter.

03:12

There’s another property on the date formatter called time style. Let’s actually set this to FullStyle. So we’re using a FullStyle for both date style and time style. When we do run that stringFromDate method on the date formatter, we get the same result up until about halfway. We have Friday, October 2, 2015. And then it tells us at 8:30:58 am CDT, how that relates to the NSDate object. And let’s ignore this first one here. Let’s actually use the date.description, since that’s a better indicator of the value that the NSDate is actually storing to actually determine all this information so that all these other classes and functions can determine all this other information.

04:01

From this we do know that it is 2015, we know that it’s October, and we know that it’s October 2. And then the time here is something that was off in the NSDates video and now says it’s 13:30:58 with no specified time zone right here, so in the UTC. But the current time is actually 8:30 in the morning not 1:30 in the afternoon. And what the NSDateFormatter is doing with this plus 0000 at the end here, is it’s taking this time zone or UTC time right here and actually getting the current device’s time zone. Either what is set or what it is determining on its own. Since we’re on my mac, I don’t remember if I set that or if it figured it out by itself. But it knows that we’re at the CDT. It’s actually taking into account this plus 0000 that this NSDate object is not in a specific time zone. It’s just calculating seconds from that date in 2001, and the date formatter is using the device’s current time zone and formatting it from that time zone value into the device’s current time zone. That it actually is taking into account the difference in hours and adjusting the current time for that.

05:22

Ok we’re back and I just wanted to show how time zones play into this whole thing. What I’ve done I’ve set the time zone on our dateFormatter object to Korean Standard Time. I just looked up some abbreviations for some random time zones and I just picked one. So we ended up with Korean Standard Time. I just ran this same function again on this same date object and we are getting a completely different time but on the same day. So we actually do know that in Korean Standard Time, or I guess just in Korea, as compared to the Midwest, that it’s actually 10:37:27 pm.

06:03

So again, the date formatter is using this time zone value at the end of this NSDate object and comparing that to it’s own time zone. Which it is not currently getting from the device, since we explicitly set it to a different time zone. It is calculating the current date and time based on that time zone from the NSDate object without any special or different time zone value over here. And you can just keep doing this with all the different time zones, it doesn’t matter.

06:37

Basically to format anything in a readable way, and again this is special exception with playgrounds I guess when we created this NSDate, you’re going to want to use the NSDateFormatter object so that you can present any dates that you need to, to the user in a readable manner.

06:57

And these are just a few of the things that you can do with the NSDateFormatter. There’s actually some other stuff where you can create a new NSDate from a string using any format that you want. You do have the specify the format with a format string. And the format string sort of looks something maybe like this. So that if I’ve passed a string into the date formatter that looked like this, then that NSDate object would point to October 2015, and since we’re not specifying a day or a time, I’ve fairly sure that it would set that NSDates time interval to October 1 at exactly midnight.

07:47

That was just another example of something you can do with NSDateFormatter and in the documentation there’s a lot more stuff. Way more than we can go over today. Thanks for watching!

Additional Info

Register to get access to additional resources and info.