Tag Archives: NSDateFormatter

How to write a Custom Initialiser in Swift

I’m used to initialising my Objective-C objects with a custom initialiser. It’s a trick I’ve learnt from the legendary Simon Allardice back in the day. It works a treat every time!

Turns out it’s a Cocoa and Cocoa Touch design pattern, and the principle can be applied similarly in Swift, with a couple of important differences.

Or shall I say pitfalls?

Continue reading

How to create an NSDate object from a string such as 22/04/2013

Here’s how we can do this, with the help of our old friend the NSDateFormatter:

    // here we have a date
    NSString *dateString = @"11/01/1989";
    // convert it into an NSDate object
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"dd/MM/yyyy"];
    NSDate *theDate = [formatter dateFromString:dateString];
    // so what is that?
    NSLog(@"Your Date Object is %@", theDate);

    // Your Date Object is 1989-01-11 05:00:00 +0000

Note the lower case yyyy. In many cases, YYYY and yyyy will yield the same result, but there is a difference:

    [formatter setDateFormat:@"dd/MM/yyyy"];

    // Your Date Object is 1988-12-25 05:00:00 +0000

As Apple put it:

It uses yyyy to specify the year component. A common mistake is to use YYYY. yyyy specifies the calendar year whereas YYYY specifies the year (of “Week of Year”), used in the ISO year-week calendar. In most cases, yyyy and YYYY yield the same number, however they may be different. Typically you should use the calendar year.

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 this, we need to create an NSDateFormatter, tell the formatter how to expect the date, and then use its convenience method dateFromString to create the date:

NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
formatter.dateFormat = @"YYYY-MM-DD";
NSDate *myDate = [formatter dateFromString:@"2012-12-12"];

Notice that I tell the date formatter to expect the date as YYYY-MM-DD. I could also have told it to expect it as YYMMDD and then pass @”12-12-12″ in the dateFromString method.

Whichever way you do it, the date format must match your string, otherwise the method returns null.

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 a date formatter and set its format
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
formatter.dateFormat = @"MMMM";
NSLog(@"The current month is %@", [formatter stringFromDate:myDate]);

The MMMM tells the date formatter to display the full month, no matter how many letters it has. You can also use:

  • M for the month number (1 for January or 12 for December)
  • MM for the two-digit month number (01 for January)
  • MMM for the abbreviated month number (Jan for January)
  • MMMM for the full month number (such as January)
  • MMMMM for just the first letter (J for January… not sure who would need this though)

The Unicode Date Format can do amazing things with all aspects of times and dates – check out the complete compendium here:

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.