Friday, May 10, 2013

NSDateFormatter setCalendar example ios

setCalendar:

Sets the calendar for the receiver.
- (void)setCalendar:(NSCalendar *)calendar
Parameters
calendar
The calendar for the receiver.
  • Example of [NSDateFormatter setCalendar]
  • NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterLongStyle];
    [formatter setTimeStyle:NSDateFormatterNoStyle];
    [formatter setCalendar:gregorian]; //this is usually unnecessary; it's here for clarity
    
    NSLog(@"date: %@", [formatter stringFromDate:date]);
    
    [formatter setCalendar:hebrew];
    
    NSLog(@"hebrew: %@", [formatter stringFromDate:hebrewDate]);
    [formatter release];
  • Example of [NSDateFormatter setCalendar]
  • NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterLongStyle];
    [formatter setTimeStyle:NSDateFormatterNoStyle];
    [formatter setCalendar:hebrew];
    
    NSLog(@"hebrew: %@", [formatter stringFromDate:[NSDate date]]);
    [formatter release];
  • Example of [NSDateFormatter setCalendar]

NSDate *now = [NSDate date];

NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setCalendar:gregorianCalendar];
[formatter setDateStyle:NSDateFormatterFullStyle];
[formatter setTimeStyle:NSDateFormatterFullStyle];

NSString *formattedDate = [formatter stringFromDate:now];

NSLog(@"%@", formattedDate);

[gregorianCalendar release];
[formatter release];