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.
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 |
---|---|
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.” | |
Specifies how the morning and afternoon designations are printed. The default is AM and PM. | |
Specifies the names for the months. | |
Specifies the abbreviations for the months. | |
Specifies the names for the days of the week. | |
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).
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 ' |
| abbreviated weekday name |
| full weekday name |
| abbreviated month name |
| full month name |
| shorthand for |
| day of the month as a decimal number (01-31) |
| same as |
| milliseconds as a decimal number (000-999) |
| hour based on a 24-hour clock as a decimal number (00-23) |
| hour based on a 12-hour clock as a decimal number (01-12) |
| day of the year as a decimal number (001-366) |
| month as a decimal number (01-12) |
| minute as a decimal number (00-59) |
| AM/PM designation for the locale |
| second as a decimal number (00-59) |
| weekday as a decimal number (0-6), where Sunday is 0 |
| date using the date representation for the locale |
| time using the time representation for the locale |
| year without century (00-99) |
| year with century (such as 1990) |
| time zone name (such as Pacific Daylight Time) |
| 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 0
s (for example, %S
represents 6 seconds as 06
). You can suppress these 0
s 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) |
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 |
---|---|
Specifies how dates with times are printed, affecting strings that use the format specifiers | |
Specifies how the morning and afternoon designations are printed, affecting strings that use the | |
Specifies the names for the months, affecting strings that use the | |
Specifies the abbreviations for the months, affecting strings that use the | |
Specifies the names for the days of the week, affecting strings that use the | |
Specifies the abbreviations for the days of the week, affecting strings that use the |
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:
.
© 2002, 2007 Apple Inc. All Rights Reserved. (Last updated: 2007-09-04)