Tuesday, March 1, 2011

How to get month & year from NSDate?

Hi. Is there any way/ method to seperate month & year form NSDate?

I just need to display current month & year? Any Example?

From stackoverflow
  • This could be done by using NSDateComponents

    Nic : Thanks Espo. I got the Solution :
    Nic : NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
    Nic : [dateformatter setDateFormat:@"MM/dd/yyyy hh:mm a"];
    Nic : NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    Nic : NSDateComponents *currentDateComponents = [gregorian components:( NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSWeekCalendarUnit) fromDate:current_date];
    Nic : NSLog(@"- current components year = %i , month = %i , week = % i, weekday = %i", [currentDateComponents year], [currentDateComponents month], [currentDateComponents week], [currentDateComponents weekday]);

0 comments:

Post a Comment