Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Although the NSDateFormatter class has evolved a great deal from its early days and now offers highly customizable elements that can be localized for various calendars and cultures, it helps to have a quick reference on hand for the most common date and time formats. Table 11-1 provides that reference, listing the most commonly used default codes. These codes are like the ones used in Recipe 11-10 to format the results from the date picker’s date property for the midscreen label. Listing 11-1 uses the formats from Table 11-1 to create an NSString utility that converts a string into a date using a format of Month-Day-Year (for example, @"05-22-1934").
| Type | Code | Notes |
|---|---|---|
| Day of month | d | 1 to 31, no leading zeros |
| dd | 01 to 31, uses leading zeros | |
| Month | M L | 1 to 12, numeric month value, no leading zeros |
| MM LL | 01 to 12, numeric month value, leading zeros | |
| MMM LLL | Jan to Dec, three-letter month abbreviation | |
| MMMM LLLL | January to December, full month name | |
| Year | y u | Four-digit year (e.g., 2009) |
| yy | Two-digit year (e.g., 09) | |
| Hour | h K | 1 to 12, no leading zeros |
| hh KK | 01 to 12, leading zeros | |
| H k | 0 to 23, 24-hour clock, no leading zeros | |
| HH kk | 00 to 23, 24-hour clock, leading zeros | |
| Minutes | m | 0 to 59, no leading zeros |
| mm | 00 to 59, leading zeros | |
| Seconds | s | 0 to 59, no leading zeros |
| ss | 0 to 59, leading zeros | |
| AM/PM | a | |
| Day of Week | ccc EEE | Sun through Sat, three-letter abbreviations |
| cccc EEEE | Sunday through Saturday, full names | |
| c e | Ordinal day of week (0–7) | |
| cc ee | Ordinal day of week (00–07) | |
| Week of Month | F | 1–5, no leading zeros |
| FF | 01–05, leading zeros | |
| Day of Year | D | 1–366, no leading zeros |
| DD | 01–366, one leading zero | |
| DDD | 001–366, two leading zeros | |
| Week of Year | w | 1–52, no leading zeros |
| ww | 01-51, one leading zero | |
| Millisecond of Day | A | 0–86399999, no padding |
| Astronomical Julian Day Number | g | Number of days since 1 January 4713 BCE |
| Era | G | BC, AD—Christian year notation |
| GGGG | Before Christ, Anno Domini—Christian year notation | |
| Quarter | q Q | 1–4, quarter of year |
| qq QQ | 01–04, one leading zero | |
| qqq QQQ | Q1, Q2, Q3, Q4 | |
| qqqq QQQQ | 1st quarter, 2nd quarter, 3rd quarter, 4th quarter | |
| Time Zone | v | Two-letter time zone (e.g., MT) |
| V z | Three-letter time zone (e.g., MDT) | |
| vv
VV ZZZZ | RFC 822 time zone offset from Greenwich Mean Time (e.g., GMT-06:00) | |
| vvvv | Time zone name (e.g., Mountain Time) | |
| VVVV | Time zone location (e.g., United States [Denver]) | |
| zzzz | Full time zone name (e.g., Mountain Daylight Time) | |
| Z | GMT offset (e.g., -0600) | |
| Other characters | :
- / | Colon, hyphen, slash |