Swift Components Tour – NSDates

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 NSDates (or data)!

Kyle Roberts
Swift Guru at Large

Kyle's Series


Tap on time to skip ahead


Hello world! Kyle here with Brax.tv. And we are in the Swift Components Tour on the SteppersViewController. And we are going to talk about dates. Now, when you’re talking about dates, I would say that there are three important fields or – it’s not classes because there’s more than three classes – but I guess we can say classes because there are three important classes. And a few maybe other little tidbits.


When you’re working with dates in code, most likely the first step or the basis of all this is going to be an NSDate object. In viewDidLoad of the SteppersViewController I actually have examples of all three of these different parts of dates in Swift that I’m going to talk about. And the first one is NSDate. We aren’t using NSDate very much in viewDidLoad right here, in these two little blocks of code. But I am just using NSDate twice to reference the exact date and time of now. So you can actually see the date that I am recording this video.


In Swift when you a new date like this, it’s going to create an NSDate object with a reference to the exact moment that that object was created. And you can customize it by passing in some different parameters to maybe get a different date in the future or the past. When you just initialize an NSDate like this, then the time and date that it is pointing to is the moment that it was created.


Real quick, the other two parts of dates in Swift that we’re not going to talk about today, but that will be covered in the next two videos are date formatters, NSDateFormatter, right here, and NSCalendar. And you can see that we also have an NSDateComponents object here that we’re using but I would lump that into the same section or even video as NSCalendar. And we’ll see why when we get there. But we’re talking about just NSDates here.


So when you create an NSDate, it’s not a very pretty or usable object. And that is because the way that the NSDate stores its time is through an absolute time interval reference to exactly midnight on January 1, 2001, in UTC time. So that means that no matter where you are in the world, an NSDate is going to look like a giant number.


OK we’re back and I’ve actually created a playground that I called DatesPlayground. I don’t know if I will commit this to the repository but it’s a great place for us to work right now. This didn’t actually show what I wanted too, and there’s obviously some additional calculating going on here. But when you create that NSDate it’s just going to be a big long number that most of the time when you see that big long number it’s going to be formatted into a different way. And we can see here that we can see that it has been formatted to show us the month with a shortened abbreviation of the month, then the day, the year, and the time, 8:17 in the morning.


I’m going to see if I can actually get a nasty year formatted here, and I actually just got it with date.description. And the description is a property that is I think on NSObject, so pretty much any object that you have in Swift is going to have this description object, or description property. What happens is when we would NSLog a date, and we can actually do that here – if I can type correctly – we NSLog this date, we need another parentheses. It should print the exact same thing right here. We can see how this is formatted is not even the value that an NSDate stores by itself but that it is still formatted so that it is readable but not quite formatted so that it is beautiful. We can see here again that it is 2015, October 2, but the time says 13:20:06 and it is not 1:20pm. It is 8:20am. But this plus 0000 at the end is actually what is referencing the time zone that I am in right now. And I am not in this time zone. So I guess at 8:20am in my time zone it is actually 1:20 in UTC.


If we used a date formatter or something to account for my time zone right now, then we would be able to format this so that it would say 8:20:06 and even without the a.m. and having the correct plus whatever that would be, I guess that would be a minus five hundred or minus five thousand. I’m not sure. But that’s not important.


The point I’m trying to make with this playground is an NSDate by itself is not really the most useful thing. It’s just to store the data for a moment in time. Often you would need to edit that date or view that date from the UI, you would need to spend some additional work on that. Because when your user is using your app, and they see a date they are not going to want to see it in this format. They are going to want to see something like this or even something simpler. Maybe something longer with more data. You can see that this does not have the seconds, it does not spell out the full word October, and it doesn’t have a time zone. But the point that I’m trying to make is that an NSDate is great just to store a value and to maybe compare to another value later on. But by itself in concerning the UI that I have opened here, it’s not going to be the most useful and you’re going to have to know how to use some other classes to actually get it work for you. Thanks for watching!

Additional Info

Register to get access to additional resources and info.