如何获得列表中一年中的所有月份 [英] How to get all months of a year in a list

查看:240
本文介绍了如何获得列表中一年中的所有月份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在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.

这篇关于如何获得列表中一年中的所有月份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆