Converting an NSDate to NSString can sometimes be not as simple as one would hope. NSharpDate is a NSDate category that can help you with this. It adds easy to use methods and properties to the NSDate object.

Example of a method

To get a NSString from a NSDate you need to use a NSDateFormatter. The following code is how you could do it without NSharpDate which would produce a string looking like this: 5/27/12 if the IOS device locale is US, or like this if the locale is Sweden: 2012-05-27.

NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterShortStyle];
[formatter setTimeStyle:NSDateFormatterNoStyle];
NSString *dateString = [formatter stringFromDate:date];


With NSharpDate you only need one line of code to do this:

NSDate *date = [NSDate date];
NSString *dateString = [date stringByShortDateFormat];


This is only one of the five methods added to NSDate, to see the other methods head over to the NSharpDate site.

Example of a property

To get the day of week from an nsdate you would use the following code without NSharpDate. This would give you a string like “Sunday” or “Söndag:
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"EEEE"];
NSString *dateString = [formatter stringFromDate:date];


To get the same result with NSharpDate you do the following:

NSDate *date = [NSDate date];
NSString  *day = date.DayOfWeek;


There are a few more properties added to NSDate which you can find here.

Download and setup
There are only three steps involved to get started with NSharpDate.

If you find a bug or want to add a feature to the project you are very welcome to add this code to the category. You can head over to github to fork the project and send me a pull request when you are done.