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.
// 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];
// 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'";
// sets the time zone difference formatterParams.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:7200]; // 2 hours more
dateTimeFormatter.formatterParameter = formatterParams;
[MAFDateTimeFormatterParams dateTimeFormatterParamsWithDateStyle:kCFDateFormatterShortStyle timeStyle:kCFDateFormatterShortStyle locale:nil toUTC:YES dateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss" timeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
// 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