Date Formatters Part 2

In the first post on working with dates several of the examples use the old style date format syntax. The examples work, however, I want to show an additional example that uses the ICU (International Components for Unicode) library for format strings.

Here is a short list of sample formats using ICU:

The format specifiers are quite straightforward, Y = year, M = month, etc. Changing the number of specifiers for a field, changes the output. For example, MMMM generates the full month name November, MMM results in Nov and MM outputs 11.

What follows is an example to create the following date string:

Saturday November 8, 2008:

NSDate *today = NSDate date;
NSDateFormatter *dateFormat = NSDateFormatter alloc init;
dateFormat setDateFormat:@"EEEE MMMM d, YYYY";
NSString *dateString = dateFormat stringFromDate:today;
dateFormat release;

Heres another example showing the current time:

9:20 AM, PST:

NSDate *today = NSDate date; 
NSDateFormatter *dateFormat = NSDateFormatter alloc init;
dateFormat setDateFormat:@"h:mm a, zzz";
NSString *dateString = dateFormat stringFromDate:today;  
dateFormat release;

Leave a Reply

Your email address will not be published. Required fields are marked *