Friday, May 10, 2013

NSDateFormatter NSDateFormatterFullStyle example ios

NSDateFormatterStyle - NSDateFormatterFullStyle

The following constants specify predefined format styles for dates and times.
typedef enum {
   NSDateFormatterNoStyle     = kCFDateFormatterNoStyle,
   NSDateFormatterShortStyle  = kCFDateFormatterShortStyle,
   NSDateFormatterMediumStyle = kCFDateFormatterMediumStyle,
   NSDateFormatterLongStyle   = kCFDateFormatterLongStyle,
   NSDateFormatterFullStyle   = kCFDateFormatterFullStyle
} NSDateFormatterStyle;
Specifies no style.
Specifies a short style, typically numeric only, such as “11/23/37” or “3:30pm”.
Specifies a medium style, typically with abbreviated text, such as “Nov 23, 1937”.
Specifies a long style, typically with full text, such as “November 23, 1937” or “3:30:32pm”.
Specifies a full style with complete details, such as “Tuesday, April 12, 1952 AD” or “3:30:42pm PST”.
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 NSDateFormatterFullStyle]
NSString *dateFromData = @"Sun, 10 Oct 2010 01:44:00 +0000";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEE, dd MMM yyyy hh:mm:ss zzzz"];

NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
[dateFormatter setTimeZone:gmt];

NSDate * date = [dateFormatter dateFromString:dateFromData];

[dateFormatter setDateStyle:NSDateFormatterFullStyle];

NSString *formattedDateString = [dateFormatter stringFromDate:date];
Example of [NSDateFormatter NSDateFormatterFullStyle]
NSString *tempDate = [NSString stringWithString:@"6:30 PM"];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterFullStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateFormat:@"HH:mm a"];

NSDate * newDate = [dateFormatter dateFromString:tempDate];
NSString * str = [dateFormatter stringFromDate:[NSDate date]];
NSLog(@"date: %@", newDate);
NSLog(@"str: %@", str);
Example of [NSDateFormatter NSDateFormatterFullStyle]
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // ...

    // if a date formatter doesn't exist yet, create it
    if (!dateFormatter) {
        dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setLocale:[NSLocale currentLocale]];
        [dateFormatter setDateStyle:NSDateFormatterFullStyle];

    cell.dateLabel = [dateFormatter stringFromDate:note.timestamp];

    // if a time formatter doesn't exist yet, create it
    if (!timeFormatter) {
        timeFormatter = [[NSDateFormatter alloc] init];
        [timeFormatter setTimeStyle:NSDateFormatterShortStyle];

    cell.timeLabel = [timeFormatter stringFromDate:note.timestamp];
    return cell;