Class

NSDateIntervalFormatter

A formatter that creates string representations of time intervals.

Declaration

@interface NSDateIntervalFormatter : NSFormatter

Overview

An NSDateIntervalFormatter object creates user-readable strings from pairs of dates. Use a date interval formatter to create user-readable strings of the form <start> - <end> for your app’s interface, where <start> and <end> are date values that you supply. The formatter uses locale and language information, along with custom formatting options, to define the content of the resulting string. You can specify different styles for the date and time information in each date value.

To use this class, create an instance, configure its properties, and call the stringFromDate:toDate: method to generate a string. The properties of this class let you configure the calendar and specify the style to apply to date and time values. Given a current date of January 16, 2015, Configuring the Formatter Options shows how to configure a formatter object and generate the string “1/16/15 - 1/17/15”.

Listing 1

Configuring a formatter object

NSDateIntervalFormatter* formatter = [[NSDateIntervalFormatter alloc] init];
formatter.dateStyle = NSDateIntervalFormatterShortStyle;
formatter.timeStyle = NSDateIntervalFormatterNoStyle;
 
// Create two dates that are exactly 1 day apart.
NSDate* startDate = [NSDate date];
NSDate* endDate = [NSDate dateWithTimeInterval:86400 sinceDate:startDate];
 
// Use the configured formatter to generate the string.
NSString* outputString = [formatter stringFromDate:startDate toDate:endDate];

The stringFromDate:toDate: method may be called safely from any thread of your app. It is also safe to share a single instance of this class from multiple threads, with the caveat that you should not change the configuration of the object while another thread is using it to generate a string.

Topics

Formatting a String

- stringFromDate:toDate:

Returns a formatted string based on the specified start and end dates.

Configuring the Formatter Options

dateStyle

The style to use when formatting day, month, and year information.

timeStyle

The style to use when formatting hour, minute, and second information.

dateTemplate

The template for formatting one date and time value.

calendar

The calendar to use for date values.

locale

The locale to use when formatting date and time values.

timeZone

The time zone with which to specify time values.

Constants

NSDateIntervalFormatterStyle

Formatting styles for individual date and time values.

Relationships

Inherits From

See Also

Date Formatting

NSDateFormatter

A formatter that converts between dates and their textual representations.

NSDateComponentsFormatter

A formatter that creates string representations of quantities of time.

NSISO8601DateFormatter

A formatter that converts between dates and their ISO 8601 string representations.