CFDateFormatter Reference
| Derived from | |
| Framework | CoreFoundation/CoreFoundation.h |
| Companion guide | |
| Declared in | CFDateFormatter.h CFLocale.h |
Overview
CFDateFormatter objects format the textual representations of CFDate and CFAbsoluteTime objects, and convert textual representations of dates and times into CFDate and CFAbsoluteTime objects. You can express the representation of dates and times very flexibly, for example “Thu 22 Dec 1994” is just as acceptable as “12/22/94.” You specify how strings are formatted and parsed by setting a format string and other properties of a CFDateFomatter object.
The format of the format string itself is defined by Unicode Technical Standard #35; the version of the standard used varies with release of the operating system, and is described in “Introduction to Data Formatting Programming Guide For Cocoa”.
Functions by Task
Creating a Date Formatter
Configuring a Date Formatter
Parsing Strings
Creating Strings From Data
-
CFDateFormatterCreateStringWithAbsoluteTime -
CFDateFormatterCreateStringWithDate -
CFDateFormatterCreateDateFormatFromTemplate
Getting Information About a Date Formatter
-
CFDateFormatterCopyProperty -
CFDateFormatterGetDateStyle -
CFDateFormatterGetFormat -
CFDateFormatterGetLocale -
CFDateFormatterGetTimeStyle
Getting the CFDateFormatter Type ID
Functions
CFDateFormatterCopyProperty
Returns a copy of a date formatter’s value for a given key.
CFTypeRef CFDateFormatterCopyProperty ( CFDateFormatterRef formatter, CFStringRef key );
Parameters
- formatter
The date formatter to examine.
- key
The property key for the value to obtain. See “Date Formatter Property Keys” for a description of possible values for this parameter.
Return Value
A CFType object that is a copy of the property value for key, or NULL if there is no value specified for key. Ownership follows the Create Rule.
Availability
- Available in iOS 2.0 and later.
Declared In
CFDateFormatter.hCFDateFormatterCreate
Creates a new CFDateFormatter object, localized to the given locale, which will format dates to the given date and time styles.
CFDateFormatterRef CFDateFormatterCreate ( CFAllocatorRef allocator, CFLocaleRef locale, CFDateFormatterStyle dateStyle, CFDateFormatterStyle timeStyle );
Parameters
- alloc
The allocator to use to allocate memory for the new object. Pass
NULLorkCFAllocatorDefaultto use the current default allocator.- locale
The locale to use for localization. If
NULLuses the default system local. UseCFLocaleCopyCurrentto specify the locale of the current user.- dateStyle
The date style to use when formatting dates. See “Date Formatter Styles” for possible values.
- timeStyle
The time style to use when formatting times. See “Date Formatter Styles” for possible values.
Return Value
A new date formatter, localized to the given locale, which will format dates to the given date and time styles. Returns NULL if there was a problem creating the object. Ownership follows the Create Rule.
Discussion
You can use kCFDateFormatterNoStyle to suppress output for the date or time. The following code fragment illustrates the creation and use of a date formatter that only outputs the date information (memory management is omitted for clarity).
CFLocaleRef locale = CFLocaleCreate(kCFAllocatorDefault, CFSTR("en_GB")); |
CFDateFormatterRef formatter = CFDateFormatterCreate( |
kCFAllocatorDefault, locale, kCFDateFormatterMediumStyle, kCFDateFormatterNoStyle); |
CFDateRef date = CFDateCreate(kCFAllocatorDefault, 123456); |
CFStringRef dateAsString = CFDateFormatterCreateStringWithDate ( |
kCFAllocatorDefault, formatter, date); |
CFShow(dateAsString); |
// outputs "2 Jan 2001" |
Availability
- Available in iOS 2.0 and later.
Declared In
CFDateFormatter.hCFDateFormatterCreateDateFormatFromTemplate
Returns a localized date format string representing the given date format components arranged appropriately for the specified locale.
CFStringRef CFDateFormatterCreateDateFormatFromTemplate ( CFAllocatorRef allocator, CFStringRef template, CFOptionFlags options, CFLocaleRef locale );
Parameters
- allocator
The allocator to use to allocate memory for the new object. Pass
NULLorkCFAllocatorDefaultto use the current default allocator.- template
A string containing date format patterns (such as “MM” or “h”).
For full details, see Unicode Technical Standard #35.
- options
No options are currently defined—pass
0.- locale
The locale for which the template is required.
Return Value
A localized date format string representing the date format components given in template, arranged appropriately for the locale specified by locale. Ownership follows the Create Rule.
The returned string may not contain exactly those components given in template, but may—for example—have locale-specific adjustments applied.
Discussion
Different locales have different conventions for the ordering of date components. You use this method to get an appropriate format string for a given set of components for a specified locale (typically you use the current locale—see CFLocaleCopyCurrent).
The following example shows the difference between the date formats for British and American English:
CFStringRef dateComponents = CFSTR("yMMMMd"); |
CFLocaleRef usLocale = CFLocaleCreate(NULL, CFSTR("en_US")); |
CFStringRef usDateFormatString = |
CFDateFormatterCreateDateFormatFromTemplate(NULL, dateComponents, 0, usLocale); |
// Date format for English (United States): MMMM d, y |
CFLocaleRef gbLocale = CFLocaleCreate(NULL, CFSTR("en_GB")); |
CFStringRef gbDateFormatString = |
CFDateFormatterCreateDateFormatFromTemplate(NULL, dateComponents, 0, gbLocale); |
// Date format for English (United Kingdom): d MMMM y |
Availability
- Available in iOS 4.0 and later.
Declared In
CFDateFormatter.hCFDateFormatterCreateDateFromString
Returns a date object representing a given string.
CFDateRef CFDateFormatterCreateDateFromString ( CFAllocatorRef allocator, CFDateFormatterRef formatter, CFStringRef string, CFRange *rangep );
Parameters
- alloc
The allocator to use to allocate memory for the new object. Pass
NULLorkCFAllocatorDefaultto use the current default allocator.- formatter
The date formatter object to use to parse string.
- string
The string that contains the date.
- rangep
A reference to the range within the string specifying the substring to be parsed. If
NULL, the whole string is parsed. Upon return, contains the range that defines the extent of the parse (may be less than the given range).
Return Value
A new date that represents string, or NULL if there was a problem creating the object. Ownership follows the Create Rule.
Availability
- Available in iOS 2.0 and later.
Declared In
CFDateFormatter.hCFDateFormatterCreateStringWithAbsoluteTime
Returns a string representation of the given absolute time using the specified date formatter.
CFStringRef CFDateFormatterCreateStringWithAbsoluteTime ( CFAllocatorRef allocator, CFDateFormatterRef formatter, CFAbsoluteTime at );
Parameters
- alloc
The allocator to use to allocate memory for the new object. Pass
NULLorkCFAllocatorDefaultto use the current default allocator.- formatter
The date formatter object that specifies the format of the returned string.
- at
The absolute time for which to generate a string representation.
Return Value
A new string that represents at in the specified format. Returns NULL if there was a problem creating the object. Ownership follows the Create Rule.
Availability
- Available in iOS 2.0 and later.
Declared In
CFDateFormatter.hCFDateFormatterCreateStringWithDate
Returns a string representation of the given date using the specified date formatter.
CFStringRef CFDateFormatterCreateStringWithDate ( CFAllocatorRef allocator, CFDateFormatterRef formatter, CFDateRef date );
Parameters
- alloc
The allocator to use to allocate memory for the new object. Pass
NULLorkCFAllocatorDefaultto use the current default allocator.- formatter
The date formatter object that specifies the format of the returned string.
- date
The date object for which to create a string representation.
Return Value
A new string that represents date in the specified format. Returns NULL if there was a problem creating the object. Ownership follows the Create Rule.
Availability
- Available in iOS 2.0 and later.
Declared In
CFDateFormatter.hCFDateFormatterGetAbsoluteTimeFromString
Returns an absolute time object representing a given string.
Boolean CFDateFormatterGetAbsoluteTimeFromString ( CFDateFormatterRef formatter, CFStringRef string, CFRange *rangep, CFAbsoluteTime *atp );
Parameters
- formatter
The date formatter object to use to parse string.
- string
The string that contains the time to be parsed.
- rangep
Reference to the range within the string specifying the substring to be parsed. If
NULL, the whole string is parsed. On return, the range that defines the extent of the parse (may be less than the given range).- atp
An absolute time value, returned by reference, that represents string. Ownership follows the Get Rule.
Return Value
true if the string was parsed successfully, otherwise false.
Availability
- Available in iOS 2.0 and later.
Declared In
CFDateFormatter.hCFDateFormatterGetDateStyle
Returns the date style used to create the given date formatter object.
CFDateFormatterStyle CFDateFormatterGetDateStyle ( CFDateFormatterRef formatter );
Parameters
- formatter
The date formatter to examine.
Return Value
The date style used to create formatter.
Availability
- Available in iOS 2.0 and later.
Declared In
CFDateFormatter.hCFDateFormatterGetFormat
Returns a format string for the given date formatter object.
CFStringRef CFDateFormatterGetFormat ( CFDateFormatterRef formatter );
Parameters
- formatter
The date formatter to examine.
Return Value
The format string for formatter as was specified by calling the CFDateFormatterSetFormat function, or derived from the date formatter’s date or time styles. Ownership follows the Get Rule.
Availability
- Available in iOS 2.0 and later.
Declared In
CFDateFormatter.hCFDateFormatterGetLocale
Returns the locale object used to create the given date formatter object.
CFLocaleRef CFDateFormatterGetLocale ( CFDateFormatterRef formatter );
Parameters
- formatter
The date formatter object to examine.
Return Value
The locale object used to create formatter. Ownership follows the Get Rule.
Availability
- Available in iOS 2.0 and later.
Declared In
CFDateFormatter.hCFDateFormatterGetTimeStyle
Returns the time style used to create the given date formatter object.
CFDateFormatterStyle CFDateFormatterGetTimeStyle ( CFDateFormatterRef formatter );
Parameters
- formatter
The date formatter to examine.
Return Value
The time style used to create formatter.
Availability
- Available in iOS 2.0 and later.
Declared In
CFDateFormatter.hCFDateFormatterGetTypeID
Returns the type identifier for CFDateFormatter.
CFTypeID CFDateFormatterGetTypeID ( void );
Return Value
The type identifier for the CFDateFormatter opaque type.
Availability
- Available in iOS 2.0 and later.
Declared In
CFDateFormatter.hCFDateFormatterSetFormat
Sets the format string of the given date formatter to the specified value.
void CFDateFormatterSetFormat ( CFDateFormatterRef formatter, CFStringRef formatString );
Parameters
- formatter
The date formatter to modify.
- formatString
The format string for formatter. The syntax of this string is defined by Unicode Technical Standard #35..
Discussion
The format string may override other properties previously set using other functions. If this function is not called, the default value of the format string is derived from the date formatter’s date and time styles.
Availability
- Available in iOS 2.0 and later.
Declared In
CFDateFormatter.hCFDateFormatterSetProperty
Sets a date formatter property using a key-value pair.
void CFDateFormatterSetProperty ( CFDateFormatterRef formatter, CFStringRef key, CFTypeRef value );
Parameters
- formatter
The date formatter to modify.
- key
The name of the property to set. See “Date Formatter Property Keys” for a description of possible values for this parameter.
- value
The value for key. This should be a CFType object corresponding to the specified key.
Availability
- Available in iOS 2.0 and later.
Declared In
CFDateFormatter.hData Types
CFDateFormatterRef
A reference to a CFDateFormatter object.
typedef struct __CFDateFormatter *CFDateFormatterRef;
Availability
- Available in iOS 2.0 and later.
Declared In
CFDateFormatter.hCFDateFormatterStyle
Data type for predefined date and time format styles.
typedef CFIndex CFDateFormatterStyle;
Discussion
For possible values, see “Date Formatter Styles.”
Availability
- Available in iOS 2.0 and later.
Declared In
CFDateFormatter.hConstants
Date Formatter Styles
Predefined date and time format styles.
enum {
kCFDateFormatterNoStyle = 0,
kCFDateFormatterShortStyle = 1,
kCFDateFormatterMediumStyle = 2,
kCFDateFormatterLongStyle = 3,
kCFDateFormatterFullStyle = 4
};
Constants
kCFDateFormatterNoStyleSpecifies no output.
You use this constant to suppress output for the date or time (see
CFDateFormatterCreatefor more details).Available in iOS 2.0 and later.
Declared in
CFDateFormatter.h.kCFDateFormatterShortStyleSpecifies a short style, typically numeric only, such as “11/23/37” or “3:30pm”.
Available in iOS 2.0 and later.
Declared in
CFDateFormatter.h.kCFDateFormatterMediumStyleSpecifies a medium style, typically with abbreviated text, such as “Nov 23, 1937”.
Available in iOS 2.0 and later.
Declared in
CFDateFormatter.h.kCFDateFormatterLongStyleSpecifies a long style, typically with full text, such as “November 23, 1937” or “3:30:32pm”.
Available in iOS 2.0 and later.
Declared in
CFDateFormatter.h.kCFDateFormatterFullStyleSpecifies a full style with complete details, such as “Tuesday, April 12, 1952 AD” or “3:30:42pm PST”.
Available in iOS 2.0 and later.
Declared in
CFDateFormatter.h.
Discussion
The format for these date and time styles is not exact because they depend on the locale, user preference settings, and the operating system version. Do not use these constants if you want an exact format, for example if you are parsing an external data file which contains date information in a fixed format. There are several different “lengths” of the formats:
"long" era names, for example "Anno Domini" instead of "AD"
"very short" names for months and weekdays; for example, "F" instead of "Friday"
"standalone" names for months and weekdays (for some locales or languages, a month name displayed in isolation needs to be written differently than a month name within a displayed date)
names of quarters; for example, "Q2" for a short quarter name
Declared In
CFDateFormatter.hDate Formatter Property Keys
Keys used in key-value pairs to discover and specify the value of date formatter properties—used in conjunction with CFDateFormatterCopyProperty and CFDateFormatterSetProperty.
const CFStringRef kCFDateFormatterIsLenient; const CFStringRef kCFDateFormatterTimeZone; const CFStringRef kCFDateFormatterCalendarName; const CFStringRef kCFDateFormatterDefaultFormat; const CFStringRef kCFDateFormatterTwoDigitStartDate; const CFStringRef kCFDateFormatterDefaultDate; const CFStringRef kCFDateFormatterCalendar; const CFStringRef kCFDateFormatterEraSymbols; const CFStringRef kCFDateFormatterMonthSymbols; const CFStringRef kCFDateFormatterShortMonthSymbols; const CFStringRef kCFDateFormatterWeekdaySymbols; const CFStringRef kCFDateFormatterShortWeekdaySymbols; const CFStringRef kCFDateFormatterAMSymbol; const CFStringRef kCFDateFormatterPMSymbol; const CFStringRef kCFDateFormatterLongEraSymbols; const CFStringRef kCFDateFormatterVeryShortMonthSymbols; const CFStringRef kCFDateFormatterStandaloneMonthSymbols; const CFStringRef kCFDateFormatterShortStandaloneMonthSymbols; const CFStringRef kCFDateFormatterVeryShortStandaloneMonthSymbols; const CFStringRef kCFDateFormatterVeryShortWeekdaySymbols; const CFStringRef kCFDateFormatterStandaloneWeekdaySymbols; const CFStringRef kCFDateFormatterShortStandaloneWeekdaySymbols; const CFStringRef kCFDateFormatterVeryShortStandaloneWeekdaySymbols; const CFStringRef kCFDateFormatterQuarterSymbols; const CFStringRef kCFDateFormatterShortQuarterSymbols; const CFStringRef kCFDateFormatterStandaloneQuarterSymbols; const CFStringRef kCFDateFormatterShortStandaloneQuarterSymbols; const CFStringRef kCFDateFormatterGregorianStartDate; const CFStringRef kCFDateFormatterDoesRelativeDateFormattingKey;
Constants
kCFDateFormatterIsLenientSpecifies the lenient property, a CFBoolean object where a true value indicates that the parsing of strings into date or absolute time values will be fuzzy.
The formatter will use heuristics to guess at the date which is intended by the string. As with any guessing, it may get the result date wrong (that is, a date other than that which was intended).
Available in iOS 2.0 and later.
Declared in
CFDateFormatter.h.kCFDateFormatterTimeZoneSpecifies the time zone property, a CFTimeZone object.
Available in iOS 2.0 and later.
Declared in
CFDateFormatter.h.kCFDateFormatterCalendarNameSpecifies the calendar name, a CFString object.
With OS X version 10.3,
kCFGregorianCalendaris the only possible value. With OS X version 10.4,kCFGregorianCalendarand other calendar names are specified by CFLocale.Available in iOS 2.0 and later.
Declared in
CFDateFormatter.h.kCFDateFormatterDefaultFormatThe original format string for the formatter (given the date & time style and locale specified at creation).
Available in iOS 2.0 and later.
Declared in
CFDateFormatter.h.kCFDateFormatterTwoDigitStartDateSpecifies the property representing the date from which two-digit years start, a CFDate object.
Available in iOS 2.0 and later.
Declared in
CFDateFormatter.h.kCFDateFormatterDefaultDateSpecifies the default date property, a CFDate object.
Available in iOS 2.0 and later.
Declared in
CFDateFormatter.h.kCFDateFormatterCalendarSpecifies the calendar property, a CFCalendar object.
Available in iOS 2.0 and later.
Declared in
CFDateFormatter.h.kCFDateFormatterEraSymbolsSpecifies the era symbols property, a CFArray of CFString objects.
Available in iOS 2.0 and later.
Declared in
CFDateFormatter.h.kCFDateFormatterMonthSymbolsSpecifies the month symbols property, a CFArray of CFString objects.
Available in iOS 2.0 and later.
Declared in
CFDateFormatter.h.kCFDateFormatterShortMonthSymbolsSpecifies the short month symbols property, a CFArray of CFString objects.
Available in iOS 2.0 and later.
Declared in
CFDateFormatter.h.kCFDateFormatterWeekdaySymbolsSpecifies the weekday symbols property, a CFArray of CFString objects.
Available in iOS 2.0 and later.
Declared in
CFDateFormatter.h.kCFDateFormatterShortWeekdaySymbolsSpecifies the short weekday symbols property, a CFArray of CFString objects.
Available in iOS 2.0 and later.
Declared in
CFDateFormatter.h.kCFDateFormatterAMSymbolSpecifies the AM symbol property, a CFString object.
Available in iOS 2.0 and later.
Declared in
CFDateFormatter.h.kCFDateFormatterPMSymbolSpecifies the PM symbol property, a CFString object.
Available in iOS 2.0 and later.
Declared in
CFDateFormatter.h.kCFDateFormatterLongEraSymbolsSpecifies the long era symbols property, a CFArray of CFString objects.
Available in iOS 2.0 and later.
Declared in
CFDateFormatter.h.kCFDateFormatterVeryShortMonthSymbolsSpecifies the very short month symbols property, a CFArray of CFString objects.
Available in iOS 2.0 and later.
Declared in
CFDateFormatter.h.kCFDateFormatterStandaloneMonthSymbolsSpecifies the standalone month symbols property, a CFArray of CFString objects.
Available in iOS 2.0 and later.
Declared in
CFDateFormatter.h.kCFDateFormatterShortStandaloneMonthSymbolsSpecifies the short standalone month symbols property, a CFArray of CFString objects.
Available in iOS 2.0 and later.
Declared in
CFDateFormatter.h.kCFDateFormatterVeryShortStandaloneMonthSymbolsSpecifies the very short standalone month symbols property, a CFArray of CFString objects.
Available in iOS 2.0 and later.
Declared in
CFDateFormatter.h.kCFDateFormatterVeryShortWeekdaySymbolsSpecifies the very short weekday symbols property, a CFArray of CFString objects.
Available in iOS 2.0 and later.
Declared in
CFDateFormatter.h.kCFDateFormatterStandaloneWeekdaySymbolsSpecifies the standalone weekday symbols property, a CFArray of CFString objects.
Available in iOS 2.0 and later.
Declared in
CFDateFormatter.h.kCFDateFormatterShortStandaloneWeekdaySymbolsSpecifies the short standalone weekday symbols property, a CFArray of CFString objects.
Available in iOS 2.0 and later.
Declared in
CFDateFormatter.h.kCFDateFormatterVeryShortStandaloneWeekdaySymbolsSpecifies the very short standalone weekday symbols property, a CFArray of CFString objects.
Available in iOS 2.0 and later.
Declared in
CFDateFormatter.h.kCFDateFormatterQuarterSymbolsSpecifies the quarter symbols property, a CFArray of CFString objects.
Available in iOS 2.0 and later.
Declared in
CFDateFormatter.h.kCFDateFormatterShortQuarterSymbolsSpecifies the short quarter symbols property, a CFArray of CFString objects.
Available in iOS 2.0 and later.
Declared in
CFDateFormatter.h.kCFDateFormatterStandaloneQuarterSymbolsSpecifies the standalone quarter symbols property, a CFArray of CFString objects.
Available in iOS 2.0 and later.
Declared in
CFDateFormatter.h.kCFDateFormatterShortStandaloneQuarterSymbolsSpecifies the short standalone quarter symbols property, a CFArray of CFString objects.
Available in iOS 2.0 and later.
Declared in
CFDateFormatter.h.kCFDateFormatterGregorianStartDateSpecifies the Gregorian start date property, a CFDate object.
This is used to specify the start date for the Gregorian calendar switch from the Julian calendar. Different locales switched at different times. Normally you should just accept the locale's default date for the switch.
Available in iOS 2.0 and later.
Declared in
CFDateFormatter.h.kCFDateFormatterDoesRelativeDateFormattingKeySpecifies the relative date formatting property, a CFBoolean object.
This is used to specify whether the receiver uses phrases such as “today” and “tomorrow” for the date component.
Available in iOS 4.0 and later.
Declared in
CFDateFormatter.h.
Discussion
The values for these keys are all CFType objects. The specific types for each key are specified above.
Declared In
CFDateFormatter.hCalendar Names
Calendar names used by CFDateFormatter.
const CFStringRef kCFGregorianCalendar;
Constants
kCFGregorianCalendarThe name of the calendar currently supported by the
kCFDateFormatterCalendarNameproperty.Available in iOS 2.0 and later.
Declared in
CFLocale.h.
Declared In
CFDateFormatter.h© 2003, 2012 Apple Inc. All Rights Reserved. (Last updated: 2012-09-19)