Tag Archives: NSDate

How to create an NSDate object

The easiest way to create an NSDate object is to create “right now” with our convenience method 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:

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:

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:
http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns

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:

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 normalize NSDate objects (i.e. set the time to midnight)

When you create new date using [NSDate date] (i.e. today, as in right now) your date will save the current time as well as its date.

In fact, under the hood an NSDate object is the amount of seconds that have elapsed since the 1st of January 2001 (or 1970), in milliseconds. So really it’s a massive floating point number. You can see what it is with this code:

This content is for members only.

In a nutshell, we “explode” the date into its year, month and day components, and then recreate a new object with these and no other components.

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:

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:

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

How to create an NSString from an NSDate

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.