Tag Archives: NSDate

Building a Day Counter on iOS

In this video series I’ll show you how to create a simple Day Counter on iOS, using Objective-C and Xcode 9. The idea is to set a date in a settings screen, and then see how many days have elapsed on the main screen right after launching the app.

This is a 3-Part Mini-Series: Continue reading

How to compare two NSDates

Screen Shot 2014-01-07 at 12.55.39Comparing NSDate objects should be as easy as comparing two numbers. But because NSDate objects are complex, it’s not an easy task.

Lucky for us the NSDate class has several ways of dealing with this conundrum.

Imagine in the following examples that we have the objects “today” and “myDate”, the latter of which we would like to compare to today. We want to know, is myDate earlier or later than today?

earlierDate and laterDate

The easiest two to remember (for me) are earlierDate and laterDate. Here’s how they work:

    if ([myDate laterDate:today] == myDate) {
        NSLog(@"myDate is LATER than today");
    } else {
        NSLog(@"myDate is EARLIER than today");
    // likewise:
    if ([myDate earlierDate:today] == myDate) {
        NSLog(@"myDate is EARLIER than today");
    } else {
        NSLog(@"myDate is LATER than today");

laterDate returns the NSDate object which is later, earlierDate returns the earlier one.


For those brains that are wired like a frigging computer we also have the compare method, which compares the results to three much less intuitive enumerations:

    if ([myDate compare:today] == NSOrderedAscending) {
        NSLog(@"myDate is EARLIER than today");
    if ([myDate compare:today] == NSOrderedDescending) {
        NSLog(@"myDate is LATER than today");
    if ([myDate compare:today] == NSOrderedSame) {
        NSLog(@"myDate and today are THE SAME DATE");

The latter option can be good if you need to make sure the two dates are identical.

How many days are left this year in Objective C

Screen Shot 2014-01-07 at 12.55.39Like many date related operations, this isn’t as straightforward for a computer as it is for a human brain. Besides, it needs to be time-travel save as well as future proof. Here’s how we do it:

First we create a Gregorian calendar object and extract the current year from it. Next we’ll add the date components to it that make up New Year’s Eve, which will give us a second date object.

Now that we have two dates, we compare them via the NSCalendar method fromDate:toDate. This in turn will result in a new date component from which we can extract the days.

// what year is this? who's the president?
NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorian components:NSYearCalendarUnit fromDate:today];
NSInteger currentYear = [components year];

NSLog(@"The year is %i", currentYear);

// create a date with the end of the year
[components setDay:31];
[components setMonth:12];
NSDate *newYearsEve = [gregorian dateFromComponents:components];

// determine how many days are left until the end of the year
NSDateComponents *daysLeftComponents = [gregorian components:NSDayCalendarUnit fromDate:today toDate:newYearsEve options:0];
NSInteger daysLeft = [daysLeftComponents day];

NSLog(@"Days to New Years Eve: %ld", (long)daysLeft);

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: