O'Reilly logo

Objective-C Recipes: A Problem-Solution Approach by Matthew Campbell

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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). ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required