Tag Archives: NSCalendar

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 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 add some time to an NSDate

Imagine you had an NSDate object and want to add several days to it. We can use NSDate method dateByAddingComponents for this, which takes – as you may have guessed – NSDateComponents as parameters.

In this example, let’s assume we want to know what date it is 5 days from today:

int daysToAdd = 5;
NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [[NSDateComponents alloc]init];
[components setDay:daysToAdd];
NSDate *futureDate = [gregorian dateByAddingComponents:components toDate:today options:0];

You can add as many and diverse components you like, such as

  • setEra:
  • setYear:
  • setMonth:
  • setDay:
  • setHour:
  • setMinute:
  • setSecond:
  • setWeek:
  • setWeekday:
  • setWeekdayOrdinal:
  • setQuarter:
  • setCalendar:
  • setTimeZone:
  • setWeekOfMonth:
  • setWeekOfYear:
  • setYearForWeekOfYear:

Check out the NSDateComponents Class Reference for more information.





How to determine how many days / months / years have passed between two NSDate objects

Imagine you had two NSDate objects and you’d like to find out the time interval between those dates. NSDate objects alone won’t help us out there unless we do some serious NSDateFormatting and hair pulling.

Lucky for us there are a few other classes available that will help us do this, namely NSCalendar and NSDateComponents.

 

Years, Months and Days

Here’s how you can determine how many years, months and days have passed between two NSDates:

NSDate *earlier = [[NSDate alloc]initWithTimeIntervalSinceReferenceDate:1];
NSDate *today = [NSDate date];

NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];

// pass as many or as little units as you like here, separated by pipes
NSUInteger units = NSYearCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit;

NSDateComponents *components = [gregorian components:units fromDate:earlier toDate:today options:0];

NSInteger years = [components year];
NSInteger months = [components month];
NSInteger days = [components day];

NSLog(@"Years: %ld, Months: %ld, Days: %ld", (long)years, (long)months, (long)days);

The above example returns Years: 12, Months: 2, Days: 5.

In a nutshell we split the date into “components” such as years, months and days, and let the NSDateComponents class give us those as NSIntegers (i.e. long integers). For the class to calculate this correctly we need to put our days into the context of an NSCalendar (gregorian in our case). This is important because different calendars may return different time intervals.

 

Just the Days

If you’re interested in just one particular item (days for example) just leave out any other component on this line:

// just the days
NSUInteger units = NSDayCalendarUnit;

How about other units?

You can pass as many of those fast enumeration units as you like, separated by pipes. For a full list of available values check out the NSCalendar Class Reference

 

Further Reading