Show TOC

MAFDateTimeFormatterLocate this document in the navigation structure

The MAFDateTimeFormatter formats date and time values into a format that is either explicitly specified with a string value, or, a preset locale and custom styles (long, short, medium, no style). These options are mutually exclusive, and specifying a string value takes priority: if you explicitly specify a date format, your locale and custom style settings are ignored.

To obtain a formatter using the factory, use the following code, which first instantiates a generic formatter factory object, then uses the object to create a date and time formatter by passing MAFFormatterTypeDateTime as the type parameter. The example does not set formatter parameters when constructing the dateTimeFormatter, because setting those formatting options is explicitly specified.
// I. obtaining a formatter factory
NSObject<MAFFormattingFactory>* formatterFactory = [[MAFFormatterFactory alloc] init];

// II. creating a date time formatter with specific formatter settings defined prior to using it
NSObject<MAFFormatting>* dateTimeFormatter = [formatterFactory createFormatterWithType:MAFFormatterTypeDateTime formatterParameter:nil];
[formatterFactory release];
To specify formatting options, first create a MAFDateTimeFormatterParams object, then specify each parameter value separately. This example sets the locale and date and time style instead of specifying a dateFormat; as a result, the date and time format is Hungarian, using a medium style date and time:
// III. create formatting definition
MAFDateTimeFormatterParams *formatterParams = [[[MAFDateTimeFormatterParams alloc] init] autorelease];

// EITHER specify locale with dateStyle and timeStyle
formatterParams.locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"hu"] autorelease];
formatterParams.dateStyle = kCFDateFormatterMediumStyle;
formatterParams.timeStyle = kCFDateFormatterMediumStyle;
// OR use a date format string directly specifying the format
//formatterParams.dateFormat = @"'yyyy'-'MM'-'dd'T'HH':'mm':'ss'"; 
To show only the time or only the date, set kCFDateFormatterNoStyle for timeStyle or dateStyle respectively. You can set additional options, for example, the time zone variance, by setting the number of seconds to add to or subtract from the UTC time:
// sets the time zone difference 
formatterParams.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:7200]; // 2 hours more
If you do not specify a time zone difference, the formatter shows the time that the device user’s phone is set to. After setting all formatting parameters, you can set these parameters for the date time formatter:
dateTimeFormatter.formatterParameter = formatterParams;
As an alternative, you can specify all formatting parameters while creating the MAFDateTimeFormatterParams object itself, and pass it for the formatter:
[MAFDateTimeFormatterParams dateTimeFormatterParamsWithDateStyle:kCFDateFormatterShortStyle timeStyle:kCFDateFormatterShortStyle locale:nil toUTC:YES dateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss" timeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
Next, perform the actual formatting and check the results:
// IV. format a value
NSDate* value = [NSDate date]; // NOW
NSString * formattedValue = [dateTimeFormatter formatValue:value];
NSLog(@"FORMATTED VALUE> %@", formattedValue);
This code presents the current time, adding 2 hours to the current time zone, using the Hungarian format:

FORMATTED VALUE> 2013.03.07. 19:35:57