Friday, May 10, 2013

NSDateFormatter NSDateFormatterLongStyle example ios


NSDateFormatterStyle - NSDateFormatterLongStyle

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 NSDateFormatterLongStyle]
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:NSDateFormatterLongStyle];

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

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
[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 NSDateFormatterLongStyle]
- (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:NSDateFormatterLongStyle];
    }

    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;
}