Friday, May 10, 2013

NSDateFormatter NSDateFormatterMediumStyle example ios


NSDateFormatterStyle - NSDateFormatterMediumStyle

The following constants specify predefined format styles for dates and times.
typedef enum {
   NSDateFormatterNoStyle     = kCFDateFormatterNoStyle,
   NSDateFormatterShortStyle  = kCFDateFormatterShortStyle,
   NSDateFormatterMediumStyle = kCFDateFormatterMediumStyle,
   NSDateFormatterLongStyle   = kCFDateFormatterLongStyle,
   NSDateFormatterFullStyle   = kCFDateFormatterFullStyle
} NSDateFormatterStyle;
Constants
NSDateFormatterNoStyle
Specifies no style.
NSDateFormatterShortStyle
Specifies a short style, typically numeric only, such as “11/23/37” or “3:30pm”.
NSDateFormatterMediumStyle
Specifies a medium style, typically with abbreviated text, such as “Nov 23, 1937”.
NSDateFormatterLongStyle
Specifies a long style, typically with full text, such as “November 23, 1937” or “3:30:32pm”.
NSDateFormatterFullStyle
Specifies a full style with complete details, such as “Tuesday, April 12, 1952 AD” or “3:30:42pm PST”.
Discussion
The format for these date and time styles is not exact because they depend on the locale, user preference settings, and the operating system version. Do not use these constants if you want an exact format.
Example of [NSDateFormatter NSDateFormatterMediumStyle]
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setLocale:[NSLocale currentLocale]];
NSDate *date = [NSDate date];
NSString *dateString = [dateFormatter stringFromDate:date];
Example of [NSDateFormatter NSDateFormatterMediumStyle]
- (NSString*) humanRelativeDateAndTimeAlt: (NSDate*) date;
{

NSDateFormatter* relativeDateFormatter = [[NSDateFormatter alloc] init];
[relativeDateFormatter setLocale: [NSLocale autoupdatingCurrentLocale]];
[relativeDateFormatter setTimeStyle:NSDateFormatterNoStyle]; // no time for this formatter
[relativeDateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setDoesRelativeDateFormatting:YES]; // relative dates

NSString* datePart = [[self relativeDateFormatter] stringFromDate:date];

NSDateFormatter* timeDateFormatter = [[NSDateFormatter alloc] init];
[timeDateFormatter setLocale:[NSLocale autoupdatingCurrentLocale]];
[timeDateFormatter setDateStyle: NSDateFormatterNoStyle]; // no date for this formatter
[timeDateFormatter setTimeStyle: NSDateFormatterShortStyle]; // does respect 12/24 hour setting

NSString* timePart = [timeDateFormatter stringFromDate:date];

int skipZero = 0;  // extra code to remove leading zero if present 
if ([[timePart substringToIndex:1] isEqualToString:@"0"]) skipZero = 1;

[relativeDateFormater release];
[timeDateFormatter release];

return [NSString stringWithFormat:@"%@ %@", datePart, [timePart substringFromIndex:skipZero]];
}
Example of [NSDateFormatter NSDateFormatterMediumStyle]
-(NSString *) stringFromDate:(NSDate *) date{

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];

[dateFormatter setLocale:[NSLocale currentLocale]];

NSString *dateString = [dateFormatter stringFromDate:date];

[dateFormatter release];

return dateString;
}