In PHP you have some great documentation on which format character to use in a string to get a certain output of a date. I thought this would be just as easy in xCode but I was wrong. The problem is, the official documentation of apple doesn’t tell you what specifiers are available to format your string. After searching endlessly on the internet I came across this site: Date Format Patterns. Which covers most of the format characters. For easy reference I decided to test all the date format patterns and create a table of my own in the style of the PHP documentation.
| Format Character | Description | Example Return Values |
| Day | ||
| d | Day of the month, 2 digits without leading zeros | 1 through 31 |
| dd | Day of the month, 2 digits with leading zeros | 01 through 31 |
| D | The day of the year | 1 through 366 |
| F | The day of the week in a month | 1 through 5 (eg. 2nd Wed in July) |
| g | Julian day number (number of days since 4713 BC January 1) | Example: 2451334 |
| G | Era designator abbreviated | Example: AD |
| GGGG | The full era designator name | Example: Anno Domini |
| GGGGG | A narrow textual representation of the era designator | Example: A |
| Day of the Week | ||
| e c |
ISO-8601 numeric representation of the day of the week | 1 (for Sunday) through 7 (for Saturday) |
| E ccc |
A textual representation of a day, three letters | Mon through Sun |
| EEEE cccc |
A full textual representation of the day of the week | Monday through Sunday |
| EEEEE ccccc |
A narrow textual representation of the day of the week | Monday through Sunday |
| Week | ||
| w | ISO-8601 week number of year | 1 through 52 |
| W | ISO-8601 week number of month | 1 through 4 |
| Month | ||
| M L |
Numeric representation of a month, without leading zeros | 1 through 12 |
| MM LL |
Numeric representation of a month, with leading zeros | 01 through 12 |
| MMM LLL |
A short textual representation of a month, three letters | Jan through Dec |
| MMMM LLLL |
A full textual representation of a month | January through December |
| MMMMM LLLLL |
A narrow textual representation of a month | Example: D |
| Year | ||
| y yyyy |
A full numeric representation of a year, 4 digits | Example: 2009 |
| yy | A two digit representation of a year | Example: 09 |
| Y YYYY |
A full numeric representation of a ISO year, 4 digits | Example: 2009 |
| YY | A two digit representation of a ISO year | Example: 09 |
| Time | ||
| a | Lowercase Ante meridiem and Post meridiem | AM or PM |
| h K |
12-hour format of an hour without leading zeros | 1 through 12 |
| hh KK |
12-hour format of an hour with leading zeros | 01 through 12 |
| H k |
24-hour format of an hour without leading zeros | 0 through 23 |
| HH kk |
24-hour format of an hour with leading zeros | 00 through 23 |
| m | Minutes without leading zeros | 0 through 59 |
| mm | Minutes with leading zeros | 00 through 59 |
| s | Seconds without leading zeros | 0 through 59 |
| ss | Seconds with leading zeros | 00 to 59 |
| A | Milliseconds in day | Example: 69540000 |
| Timezone | ||
| zzzz | A full textual representation of GMT timezone | Example: Central European Time |
| Z | Difference to Greenwich time (GMT) in hours in RFC 822 format | Example: +0200 |
| ZZZZ | Difference to Greenwich time (GMT) in hours in GMT format | Example: GMT+02:00 |
Example:
NSDate *today = [NSDate date]; NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss ZZZZ"]; // will output something like: 2009-05-23 16:04:37 GMT+02:00 NSLog ([dateFormatter stringFromDate:today]);