Chapter    5

Working With Dates, Times, and Timers

This chapter covers how to work with dates and timers using the Foundation framework with Objective-C.

The recipes in this chapter will show you how to:

  • Create today’s date using NSDate
  • Create custom dates using NSDateComponents
  • Compare dates
  • Convert strings to dates
  • Format dates for display on user interfaces
  • Add and subtract dates
  • Use a timer to schedule repeating and non-repeating code

5.1 Creating a Date Object for Today


You need to represent today’s date in your application.


Use the NSDate class method date to create a date object instance for the current date.

How It Works

NSDate is a class that is generally used with other classes (covered in the upcoming recipes). ...

Get Objective-C Recipes: A Problem-Solution Approach now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.