NSDateFormatter Archives

How to create an NSDate object

The easiest way to create an NSDate object is to create “right now” with our convenience method date: NSDate *myDate = [NSDate date]; But if you want to create a date object with a date such as your birthday it gets a little bit trickier, and – more importantly – much less obvious. To do […]

How to display the full month from an NSDate (such as “February”)

There was me thinking I’d have to whip out NSDateComponents and NSCalendar – but my mind works too complicated at times. All we need to do this is our good friend the NSDateFormatter and the Unicode Date Format MMMM – let’s check it out: // create today’s date NSDate *myDate = [NSDate date]; // create […]

How to create an NSString from an NSDate

NSDate *date = [NSDate date]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; [dateFormatter setDateStyle:NSDateFormatterLongStyle]; [dateFormatter setTimeStyle:NSDateFormatterLongStyle]; NSString *dateString = [dateFormatter stringFromDate:date]; First we’ll create a date object. Next we’ll create an NSDateFormatter and set how we’d like for display our date (and optionally our time). Then we’ll call the magical stringFromDate method which will create our string.