4

如何以这种特定格式“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 年 2 月等所需的输出。

4

5 回答 5

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年六月

2013 年 7 月

2013年八月

2013年九月

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);
于 2013-07-04T06:42:45.110 回答
6

斯威夫特 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
于 2017-09-12T12:23:14.057 回答
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++;

在当前年份中添加您想要在范围内的年份。我希望这能帮到您

修改代码

Month Start From 一月为此,您可以在此处设置要在日历组件中启动表单的日期,设置“30/01/2013”

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

请使用此代码,该代码将从 一月开始

 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++;
        }

  }
于 2013-07-04T05:58:03.743 回答
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];
于 2013-07-04T06:35:21.967 回答
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];

我做了一个月,你可以把它放在循环中。希望能帮助到你。

于 2013-07-04T06:15:57.597 回答