Friday, May 10, 2013

NSDateFormatter dateFormatFromTemplate example ios


dateFormatFromTemplate :options:locale:

Returns a localized date format string representing the given date format components arranged appropriately for the specified locale.
+ (NSString *)dateFormatFromTemplate:(NSString *)template options:(NSUInteger)opts locale:(NSLocale *)locale
Parameters
template
A string containing date format patterns (such as “MM” or “h”).
For full details, see Date and Time Programming Guide.
opts
No options are currently defined—pass 0.
locale
The locale for which the template is required.
Return Value of [NSDateFormatter dateFormatFromTemplate]
A localized date format string representing the date format components given in template, arranged appropriately for the locale specified by locale.
The returned string may not contain exactly those components given in template, but may—for example—have locale-specific adjustments applied.
Discussion of [NSDateFormatter dateFormatFromTemplate]
Different locales have different conventions for the ordering of date components. You use this method to get an appropriate format string for a given set of components for a specified locale (typically you use the current locale—see currentLocale).
Example of [NSDateFormatter dateFormatFromTemplate]
The following example shows the difference between the date formats for British and American English:
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
NSLocale *gbLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"];
 
NSString *dateFormat;
NSString *dateComponents = @"yMMMMd";
 
dateFormat = [NSDateFormatter dateFormatFromTemplate :dateComponents options:0 locale:usLocale];
NSLog(@"Date format for %@: %@",
    [usLocale displayNameForKey:NSLocaleIdentifier value:[usLocale localeIdentifier]], dateFormat);
 
dateFormat = [NSDateFormatter dateFormatFromTemplate :dateComponents options:0 locale:gbLocale];
NSLog(@"Date format for %@: %@",
    [gbLocale displayNameForKey:NSLocaleIdentifier value:[gbLocale localeIdentifier]], dateFormat);
 
// Output:
// Date format for English (United States): MMMM d, y
// Date format for English (United Kingdom): d MMMM y


Example of [NSDateFormatter dateFormatFromTemplate]

NSDateComponents *dateComponents = [[[NSDateComponents alloc] init] autorelease];
 [dateComponents setYear:2011];
 [dateComponents setMonth:1];
 [dateComponents setDay:1];

 NSDate *date = [[NSCalendar currentCalendar] dateFromComponents:dateComponents];

 NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
 [dateFormatter setDateFormat:[NSDateFormatter dateFormatFromTemplate:@"yyyyMMMd" options:0 locale:[NSLocale currentLocale]]];

 NSString *dateString = [dateFormatter stringFromDate:date];

 NSLog(@"Happy New Year: %@", dateString);

Example of [NSDateFormatter dateFormatFromTemplate]
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];  
NSString *longFormatWithoutYear = [NSDateFormatter dateFormatFromTemplate:@"MMMM d" options:0 locale:[NSLocale currentLocale]]; 
[dateFormatter setDateFormat:longFormatWithoutYear];
//format your date... 
//output will change according to locale. E.g. "July 9" in US or "9 de julho" in Portuguese