Much like Casey Liss of the Accidental Tech Podcast, talked about recently on the Debug podcast, I came from C# before I started working on Objective-C. One frustration he had originally with Objective-C was with getting a human readable date out of an NSDate object, when it was so easy to just type someDateTime.toString() in C#. That was also one of my earliest frustrations, and I thought it would be nice to share with my readers.
In C#, you store dates in objects of the type DateTime. The Objective-C equivalent of this is NSDate. In C#, you can simply create a specific date date with:
DateTime someDate = new DateTime(2013, 12, 31);
And that would make a DateTime object for December 31, 2013. There are a few ways to do this in Objective-C, and while they are not as easy, they do make it much more friendly to using non-Gregorian calendar systems. NSDateFormatter can both format an NSDate into a string (more on this later), but it can also attempt to parse a string with date data. The more exact way though, would probably be to use NSDateComponents.
To make a similar NSDate, you would do it thusly:
NSDateComponents *comps = [[NSDateComponents alloc] init];
comps.day = 31;
comps.month = 12;
comps.year = 2013;
NSDate *someDate = [[NSCalendar currentCalendar] dateFromComponents:comps];
Much more verbose, but it gets the job done. I basically request the NSCalendar object, to make a date (in that calendar style), that has 12 months, 31 days, and 2013 years as its components. NSCalendar currentCalendar is requesting an instance of the calendar system chosen in the device’s system preferences. Now, to actually print this out in a human readable way, you use the earlier mentioned NSDateFormatter. There are several built in styles of formatting the date, and you can also specify your own.
Here are the built in ones (with the exception of NSDateFormatterNoStyle, whose reason for not being included should be self-explanatory). These can instruct the NSDateFormatter to format a date (when used as a dateStyle), and to format time (when used as a timeStyle). These have the added benefit of adapting to the current user’s locale. The examples shown below are for an en-US locale.
NSDateFormatterStyle | Date Out | Time Out |
---|---|---|
NSDateFormatterShortStyle | 12/31/13 | 2:15 PM |
NSDateFormatterMediumStyle | Dec 31, 2013 | 2:15:09 PM |
NSDateFormatterLongStyle | December 31, 2013 | 2:15:09 PM PST |
NSDateFormatterFullStyle | Tuesday, December 31, 2013 AD | 2:15:09 PM Pacific Standard Time |
So to use these, you would use the code below:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateStyle = NSDateFormatterLongStyle;
self.someLabel.text = [formatter stringFromDate:someDate];
If you want to make your own, you would not assign a dateStyle, and instead set a format string to the dateFormat property. The full list of format patterns is available on this UnicodeTechnical Standard #35 page (this is only valid for iOS7, different releases go to different standards). The Apple documentation shows a good example of using the a custom Date Format.
[formatter setDateFormat:@"yyyy-MM-dd 'at' HH:mm"];
Which would output something like “2013-12-31 at 08:45”, if I had set a time to my date.
It’s a small topic, but certainly a frustrating one when a person is starting out with the language. I hope you found this article helpful. If you did, don’t hesitate to share this post on twitter or your social media of choice. The blog is still pretty new, and every share helps. Of course, if you have any questions, don’t hesitate to contact me on twitter @CodingExplorer, and I’ll see what I can do. Thanks!