过去和未来月份的问题

时间:2016-05-06 11:39:24

标签: ios objective-c calendar

上个月使用此代码:

if (currentmonth > 1) {
    currentmonth = currentmonth - 1;
    Presentmonth.text = (monthArray[currentmonth - 1]);
}

要获得使用此代码的未来月份:

if (currentmonth < 12) {
    currentmonth = currentmonth + 1;  
    Presentmonth.text = (monthArray[currentmonth - 1]);
}

在我的月份数组中

monthArray = [NSMutableArray arrayWithObjects:@"January", @"February", @"March", @"April", @"May", @"June", @"July"‌​, @"August", @"September", @"October", @"November", @"December", nil];

// Presentmonth.text = [monthArray objectAtIndex:currentmonth];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MMMM"];
NSDate *myDate = [[NSDate alloc]init];
stringFromDate = [formatter stringFromDate:myDate];
Presentmonth.text = [NSString stringWithFormat:@"%@", stringFromDate];

我的问题是,将May视为当前月份,然后是april,之前是june,在我的代码中,返回的未来月份是从January开始的是june。我的逻辑有什么不对?

3 个答案:

答案 0 :(得分:1)

更改以下代码。

NSDate *currentDate;

- (void)viewDidLoad {
    [super viewDidLoad];

    currentDate = [NSDate date];

    Presentmonth.text=[self getCurrentMonth];
}

- (void)previousMonth {
    NSCalendar *cal         = [NSCalendar currentCalendar];
    NSDateComponents *comps = [cal components:NSCalendarUnitMonth
                                     fromDate:currentDate];
    comps.month             -= 1;
    currentDate          = [cal dateFromComponents:comps];

    Presentmonth.text=[self getCurrentMonth];
}

- (void)nextMonth {
    NSCalendar *cal         = [NSCalendar currentCalendar];
    NSDateComponents *comps = [cal components:NSCalendarUnitMonth fromDate:currentDate];
    comps.month             += 1;
   currentDate          = [cal dateFromComponents:comps];

    Presentmonth.text=[self getCurrentMonth];

}

- (NSString *)getCurrentMonth {
    NSCalendar *cal         = [NSCalendar currentCalendar];
    NSDateComponents *comps = [cal components:NSCalendarUnitMonth
                                     fromDate:currentDate];
    NSDateFormatter *df = [[NSDateFormatter alloc] init];

    return [[df monthSymbols] objectAtIndex:(comps.month-1)];
}

希望这有帮助。

答案 1 :(得分:0)

看起来你正在减少montharray中的月份索引,而不是递增。即它应该是monthArray[currentmonth+1]而不是monthArray[currentmonth-1]。 //修改

所以,请进行以下更改
//上个月使用此代码:

if (currentmonth>1) {

    currentmonth=currentmonth-1;

    Presentmonth.text=(monthArray[currentmonth]);
}

//要获得使用此代码的未来月份:

if (currentmonth<12) {

    currentmonth=currentmonth+1;  

    Presentmonth.text=(monthArray[currentmonth]);
}

答案 2 :(得分:0)

请在前几个月和一年中试用此代码

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
    [offsetComponents setMonth:-
1]; 
    nextdate = [gregorian dateByAddingComponents:offsetComponents toDate:today options:0];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"MMMM yyyy"];
    today=nextdate;

TopMonthlbl.text = [dateFormat stringFromDate:today];
[dateFormat setDateFormat:@"MMMM"];
monthLbl.text = [dateFormat stringFromDate:nextdate];

NSRange dayRange = [[NSCalendar currentCalendar]
                    rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:nextdate]