< Previous PageNext Page > Hide TOC

Converting Dates to Strings

The first section, “String Representations of NSDate Objects,” describes some formatting methods for NSDate. The last two sections, “The Calendar Format” and “String Representations of NSCalendarDate Objects,” describes methods for formatting NSCalendarDate.

Important: The preferred way to convert a date to a string, or a string to a date, is to use a date formatter, as described in Data Formatting Programming Guide for Cocoa. For the sake of old code that was written before date formatters were available, the Objective-C interfaces for NSDate and NSCalendarDate still contain some methods for string conversions.

String Representations of NSDate Objects

To represent your date object as an NSString object, use the description... methods. The simplest method, description, prints out the date in the format YYYY-MM-DD HH:MM:SS ±HHMM, where ±HHMM represents the time zone offset in hours and minutes from GMT. (Adding the offset to the specific time yields the equivalent GMT.) To have a specific locale dictionary affect the representation of your NSDate object, use descriptionWithLocale: instead of description. The following keys in the locale dictionary affect NSDate objects:

Key

Description

NSTimeDateFormatString

Specifies how dates with times are printed. The default is to use full month names and days with a 24 hour clock, as in “Sunday, January 01, 2001 23:00:00 Pacific Standard Time.”

NSAMPMDesignation

Specifies how the morning and afternoon designations are printed. The default is AM and PM.

NSMonthNameArray

Specifies the names for the months.

NSShortMonthNameArray

Specifies the abbreviations for the months.

NSWeekDayNameArray

Specifies the names for the days of the week.

NSShortWeekDayNameArray

Specifies the abbreviations for the days of the week.

Note that NSDate’s implementation of the description method uses NSCalendarDate. NSCalendarDate does not model the transition from the Julian to the Gregorian calendar, so using NSDate’s description method yields inaccurate results for dates earlier than October 1582. If you need to describe dates earlier than the transition, you should use NSDateFormatter (as described in Data Formatting Programming Guide for Cocoa).

The Calendar Format

Each NSCalendarDate object has a calendar format associated with it. This format is a string that contains date-conversion specifiers that are very similar to those used in the standard C library function strftime(). NSCalendarDate interprets dates that are represented as strings conforming to this format. You can set the default format for an NSCalendarDate object at initialization time or using the setCalendarFormat: method. Several methods allow you to specify formats other than the one bound to the object.

The date conversion specifiers cover a range of date conventions:

Conversion Specifier

Description

%%

a '%' character

%a

abbreviated weekday name

%A

full weekday name

%b

abbreviated month name

%B

full month name

%c

shorthand for %X %x, the locale format for date and time

%d

day of the month as a decimal number (01-31)

%e

same as %d but does not print the leading 0 for days 1 through 9

%F

milliseconds as a decimal number (000-999)

%H

hour based on a 24-hour clock as a decimal number (00-23)

%I

hour based on a 12-hour clock as a decimal number (01-12)

%j

day of the year as a decimal number (001-366)

%m

month as a decimal number (01-12)

%M

minute as a decimal number (00-59)

%p

AM/PM designation for the locale

%S

second as a decimal number (00-59)

%w

weekday as a decimal number (0-6), where Sunday is 0

%x

date using the date representation for the locale

%X

time using the time representation for the locale

%y

year without century (00-99)

%Y

year with century (such as 1990)

%Z

time zone name (such as Pacific Daylight Time)

%z

time zone offset in hours and minutes from GMT (HHMM)

Note that most of the formats that specify numeric values pad the width of the value with 0s (for example, %S represents 6 seconds as 06). You can suppress these 0s using a width specifier, just as you would with, for example, printf, as illustrated by the following example.

NSCalendarDate *date = [NSCalendarDate dateWithYear:2006 month:2 day:2
                                       hour:2 minute:2 second:2
                                       timeZone:nil];
[date setCalendarFormat:@"%m (%1m), %d (%1d), %I (%1I), %j (%1j), %S (%1S)"];
NSLog(@"date: %@", date);
 
// Output: date: 02 (2), 02 (2), 02 (2), 033 (33), 02 (2)

String Representations of NSCalendarDate Objects

NSCalendarDate provides several description... methods for representing dates as strings. These methods—description, descriptionWithLocale:, descriptionWithCalendarFormat:, and descriptionWithCalendarFormat:locale:—take an implicit or explicit calendar format. The user’s locale information affects the returned string. NSCalendarDate accesses the locale information as an NSDictionary object. If you use descriptionWithLocale: or descriptionWithCalendarFormat:locale:, you can specify a different locale dictionary. The following keys in the locale dictionary affect NSCalendarDate:

Locale Key

Description

NSTimeDateFormatString

Specifies how dates with times are printed, affecting strings that use the format specifiers %c, %X, or %x. The default is to use abbreviated months and days with a 24 hour clock, as in “Sun Jan 01 23:00:00 +6 2001”.

NSAMPMDesignation

Specifies how the morning and afternoon designations are printed, affecting strings that use the %p format specifier. The default is AM and PM.

NSMonthNameArray

Specifies the names for the months, affecting strings that use the %B format specifier.

NSShortMonthNameArray

Specifies the abbreviations for the months, affecting strings that use the %b format specifier.

NSWeekDayNameArray

Specifies the names for the days of the week, affecting strings that use the %A format specifier.

NSShortWeekDayNameArray

Specifies the abbreviations for the days of the week, affecting strings that use the %a format specifier.

If you subclass NSCalendarDate and override description, you should also override descriptionWithLocale:. The stringWithFormat: method of NSString uses descriptionWithLocale: instead of description when you use the %@ conversion specifier to print an NSCalendarDate. That is, this message:

[NSString stringWithFormat:@"The current date and time are %@",
        [MyNSCalendarDateSubclass date]]

invokes descriptionWithLocale:.



< Previous PageNext Page > Hide TOC


© 2002, 2007 Apple Inc. All Rights Reserved. (Last updated: 2007-09-04)


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.