Tag Archives: NSDate

Building a Day Counter on iOS – Part 3

In this 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:

  • Part 1 is all about building the interface in Interface Builder
  • Part 2 is about coding the NSDate subtraction methods, using NSCalendar and loading/saving data using NSUserDefaults
  • Part 3 will introduce Key/Value Observing to update the first view controller as soon as the date is changed in the settings and deals with how to normalise an NSDate object.

You can find the full code on GitHub:

Happy Hacking!





Building a Day Counter on iOS – Part 2

In this 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:

  • Part 1 is all about building the interface in Interface Builder
  • Part 2 is about coding the NSDate subtraction methods, using NSCalendar and loading/saving data using NSUserDefaults
  • Part 3 will introduce Key/Value Observing to update the first view controller as soon as the date is changed in the settings and deals with how to normalise an NSDate object.

You can find the full code on GitHub:

Happy Hacking!





Building a Day Counter on iOS – Part 1

In this 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:

  • Part 1 is all about building the interface in Interface Builder
  • Part 2 is about coding the NSDate subtraction methods, using NSCalendar and loading/saving data using NSUserDefaults
  • Part 3 will introduce Key/Value Observing to update the first view controller as soon as the date is changed in the settings and deals with how to normalise an NSDate object.

You can find the full code on GitHub:

Happy Hacking!





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.

compare

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.