如何以特定格式获得一年中的所有月份

时间:2013-07-04 05:38:20

标签: ios nsdate nsdateformatter

如何以特定格式“2013年1月”等表格查看一年中的所有月份。我也需要设置起始月份。以下是我试过的代码。

int mCurrentMonth = [dateComponents month];
int mCurrentYear = [dateComponents year];

NSString * dateString = [NSString stringWithFormat: @"%d",mCurrentMonth];

NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MM"];
NSDate* myDate = [dateFormatter dateFromString:dateString];

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MMMM"];
NSString *stringFromDate = [formatter stringFromDate:myDate];
NSString *mon = [NSString stringWithFormat:@"%@ %d",stringFromDate, mCurrentYear];
NSLog(@"month:%@", mon);

以上代码仅提供所需格式的当月。 我试过的其他方式就是这个

NSDateFormatter *df = [[NSDateFormatter alloc] init];

for (int i = 0; i < 12; i++){
    NSString *monthName = [[df monthSymbols] objectAtIndex:i];
    NSLog(@"months name:%@", monthName);
}

通过这种方式,我得到所有月份的名字,但不是年份。我没有得到如何实现2013年1月2013年的期望输出等等。

5 个答案:

答案 0 :(得分:7)

试试这个:

  NSDateFormatter  *dateFormatter   = [[NSDateFormatter alloc] init];

    NSDate           *today           = [NSDate date];
    NSCalendar       *currentCalendar = [NSCalendar currentCalendar];
    NSDateComponents *yearComponents  = [currentCalendar components:NSYearCalendarUnit  fromDate:today];
    int currentYear  = [yearComponents year];
    for(int months = 0; months < 12; months++)
        NSLog(@"%@ %i",[[dateFormatter monthSymbols]objectAtIndex: months],currentYear);

输出将是:

2013年1月

2013年2月

2013年3月

2013年4月

2013年5月

2013年6月

2013年7月

2013年8月

2013年9月

2013年10月

2013年11月

2013年12月

已编辑:对于当前月份的上方输出使用以下代码:

 NSDateFormatter  *dateFormatter   = [[NSDateFormatter alloc] init];

    NSDate           *today           = [NSDate date];
    NSCalendar       *currentCalendar = [NSCalendar currentCalendar];
    NSDateComponents *yearComponents  = [currentCalendar components:NSYearCalendarUnit |    NSMonthCalendarUnit  fromDate:today];
    int currentYear  = [yearComponents year];
    int currentmonth=[yearComponents month];
    NSLog(@"current Month %d",currentmonth);
    for(int months = currentmonth - 1; months < 12; months++)
        NSLog(@"%@ %i",[[dateFormatter monthSymbols]objectAtIndex: months],currentYear);

答案 1 :(得分:3)

请尝试此代码,这可能会对您有所帮助

NSDateFormatter  *dateFormatter   = [[NSDateFormatter alloc] init];
NSDate           *today           = [NSDate date];
NSCalendar       *currentCalendar = [NSCalendar currentCalendar];

NSDateComponents *monthComponents = [currentCalendar components:NSMonthCalendarUnit fromDate:today];
int currentMonth = [monthComponents month];

NSDateComponents *yearComponents  = [currentCalendar components:NSYearCalendarUnit  fromDate:today];
int currentYear  = [yearComponents year];
int nextYear     = currentYear + 1;

int months  = 1;
int year;
for(int m = currentMonth; months < 12; m++){

    int nextMonth = m % 12;

    if(nextMonth < currentMonth){
        year = nextYear;
    } else {
        year = currentYear;
    }

    NSLog(@"%@ %i",[[dateFormatter monthSymbols] objectAtIndex: nextMonth],year);

    months++;

在当前年份中添加您想要的范围内的年份。我希望这会对你有所帮助

修改代码

月从1月开始为此,您可以在日历组件中设置要开始表单的日期,此处设置为“30/01/2013”​​

NSDate *dateStart= [dateFormatter dateFromString:@"30/01/2013"];
NSDateComponents *monthComponents = [currentCalendar components:NSMonthCalendarUnit fromDate:dateStart]

请使用此代码启动moth形式 1月

 NSDateFormatter  *dateFormatter   = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd/mm/yyyy"];
NSDate           *dateStart           = [dateFormatter dateFromString:@"30/01/2013"];
NSCalendar       *currentCalendar = [NSCalendar currentCalendar];

NSDateComponents *monthComponents = [currentCalendar components:NSMonthCalendarUnit fromDate:dateStart];
int currentMonth = [monthComponents month];

NSDateComponents *yearComponents  = [currentCalendar components:NSYearCalendarUnit  fromDate:dateStart];
int currentYear  = [yearComponents year];

int months  = 1;
int nextMonth=0;
int year;

    for(int m = currentMonth; months < 13; m++){
    year = currentYear;
   NSLog(@"%@ %i",[[dateFormatter monthSymbols] objectAtIndex: nextMonth],year);
    nextMonth =m % 12;
    if(nextMonth <= currentMonth ){
        year = currentYear+1;

    }       
    months++;
        if(months>12){
            months=1;
            currentYear++;
        }

  }

答案 2 :(得分:3)

Swift 4.2

let dateFormatter = DateFormatter()
let today = Date()
let currentCalendar = Calendar.current
var yearComponents: DateComponents? = currentCalendar.dateComponents([.year], from: today)
let currentYear = Int(yearComponents!.year!)
for months in 0..<12 {
    print("\(dateFormatter.shortMonthSymbols[months]) \(currentYear)")
}

输出:

Jan 2017
Feb 2017
Mar 2017
Apr 2017
May 2017
Jun 2017
Jul 2017
Aug 2017
Sep 2017
Oct 2017
Nov 2017
Dec 2017

答案 3 :(得分:1)

NSInteger startingMonth = 1;
NSInteger startingYear = 2013;

// we'll need this in several places
NSCalendar *cal = [NSCalendar currentCalendar];

// build the first date (in the starting month and year)
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setMonth:startingMonth];
[comps setYear:startingYear];
[comps setDay:1];
NSDate *date = [cal dateFromComponents:comps];

// this is our output format
NSDateFormatter *formatter = [[NSDateFormatter alloc] init]
[formatter setDateFormat:@"MMMM YYYY"];

// we need NSDateComponents for the difference, i.e. at each step we
// want to go one month further
NSDateComponents *comps2 = [[NSDateComponents alloc] init];
[comps2 setMonth:1];

for (int = 0; i < 12; i++) {
    NSLog("%@", [formatter stringFromDate:date]);

    // add 1 month to date
    date = [cal dateByAddingComponents:comps2 toDate:date options:0];
}

[comps2 release];
[fomatter release];
[comps release];

答案 4 :(得分:0)

试试这个

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components = [[NSDateComponents alloc] init];

int year=2010,month=5;
[components setYear:year];
[components setMonth:month];
NSDate *help = [[NSDate alloc]init];
help = [calendar dateFromComponents:components];


NSString *dateStr =[NSString stringWithFormat:@"%@",help];
NSDateFormatter *dtF = [[NSDateFormatter alloc] init];
[dtF setDateFormat:@"yyyy-MM-dd HH:mm:ss ZZZZ"];
NSDate *dt = [dtF dateFromString:dateStr];
NSDateFormatter *dateFormatStr = [[NSDateFormatter alloc] init];
[dateFormatStr setDateFormat:@"MMMM-yyyy"];
NSString *strDate = [dateFormatStr stringFromDate:dt];

NSLog(@"%@",strDate);

         OR

//在您的代码中将所有代码保存在for循环中并根据需要设置月份。

[components setYear:year];
[components setMonth:month];

我做了一个月,你可以把它放进去。希望它有所帮助。