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

Problem

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

Solution

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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.