如何获得列表中一年中的所有月份

问题描述:

如何在UITableView的列表中获取一年中的所有月份.我想将一年中的所有月份都放在一个数组中,以便我cxan在表格视图中显示出来.我这样做是为了获得一周的约会时间.

How to get all months of a year in a list for UITableView. I want to get all the months of a year in an array so that i cxan display that in table view. I did this way for getting date of a week.

 NSCalendar* calendar = [NSCalendar currentCalendar];// Init calendar
NSDateComponents* comps = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate: date]; // Get necessary date components

NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEE, dd"];
// Week days change from 1 to 7, Sunday is the 1st, Saturday - the last one.
for (int i = 1; i <= 7; i++){
    [comps setWeekday:i];
    NSDate *tDate = [calendar dateFromComponents:comps];
    // NSString *day=[dateFormatter stringFromDate:tDate];
    [self.mArrDates addObject:tDate];

}
NSLog(@"dates for week%@", self.mArrDates);

只需使用此:

NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
myArray = [df monthSymbols];

您还可以使用:

– shortMonthSymbols
– veryShortMonthSymbols
– shortStandaloneMonthSymbols

在此处查找所有信息:

NSdateFormatter指南

要获取当前年份,请使用此:

To get the current year, use this:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy"];
NSString *yearString = [formatter stringFromDate:[NSDate date]];
[formatter release];

如果要再一年,只需使用其他NSDate.

If you want another year, just use a different NSDate.