Apple Developer Connection
Member Login Log In | Not a Member? Contact ADC

< Previous PageNext Page > Hide TOC

Date Format String Syntax (Mac OS X Versions 10.0 to 10.3)

Date formatters format the textual representation of cells that contain date objects (including Gregorian dates), and convert textual representations of dates and times into date objects. You can express the representation of dates and times very flexibly: “Thu 22 Dec 1994” is just as acceptable as “12/22/94”. In Cocoa, with natural-language processing for dates enabled, users can also express dates colloquially, such as “today,” “day after tomorrow,” and “a month from today.”

Format String Syntax

You create date formatter objects by specifying a format string using strftime-style conversion specifiers. For example, the format string "%m/%d/%y" yields strings such as "01/02/01", and "%1m/%1d/%Y" yields strings such as "1/2/2001", as illustrated in the following example.

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc]
     initWithDateFormat:@"%1m/%1d/%Y" allowNaturalLanguage:NO] autorelease];
NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:118800];
NSString *formattedDateString = [dateFormatter stringFromDate:date];
NSLog(@"formattedDateString: %@", formattedDateString);
 
// Output: formattedDateString: 1/2/2001

You use the format string is used to specify both the input to and the output from date formatter objects. All the date formatter objects in both Foundation and Core Foundation use this syntax when specifying the format string. The date conversion specifiers in the table below cover a range of date conventions (differences from the format used by strftime() are noted in parentheses):

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 (unlike strftime(), does not print a leading space)

%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, including the time zone (produces different results from strftime())

%X

Time using the time representation for the locale (produces different results from strftime())

%y

Year without century (00-99)

%Y

Year with century (such as 1990)

%Z

Time zone name (such as Pacific Daylight Time; produces different results from strftime())

%z

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



< Previous PageNext Page > Hide TOC


Last updated: 2007-03-20




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.
Get information on Apple products.
Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Copyright © 2007 Apple Inc.
All rights reserved. | Terms of use | Privacy Notice