iOS 和 Objective-C:每季度重复一次事件 [英] iOS and Objective-C: Repeating an event every quarter

查看:77
本文介绍了iOS 和 Objective-C:每季度重复一次事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要每季度"重复一次事件(我认为这意味着大约提前 3 个月).所以我可以期望这会改变日期(但它不会):

I need to have an event repeat every "quarter" (which I assume means moving up 3 months approximately). So I could expect this to move the date (but it doesn't):

    NSDateComponents *component = [[NSDateComponents alloc] init]; // quarter
    component.quarter = 1;

    self.todoStartDate = [[NSCalendar currentCalendar] dateByAddingComponents: component toDate:self.todoStartDate options: 0];

向日期添加季度"有什么问题吗?添加一天或一周时效果很好,但不能添加季度".

Is there anything wrong with adding a "quarter" to a date? It works fine when adding a day or a week, but not with a "quarter".

推荐答案

我遇到了同样的问题,不知道为什么季度不行,所以解决方法是按月替换季度 * 3.下面演示如何倒退四分之三:

I faced the same issue, don't know why the quarter doesn't work, so workaround by replacing quarter by month * 3. The following demonstrates how to get three quarters backward:

        NSDateComponents *dayComponent = [[NSDateComponents alloc] init];
        NSCalendar *theCalendar = [NSCalendar currentCalendar];

        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        dateFormatter.dateFormat = @"QQQ yyyy";
        NSDate *currentDate = [NSDate date];

        for (int i = -2; i <= 0; i++) {
            //dayComponent.quarter = i;
            dayComponent.month = i*3;

            NSDate *date = [theCalendar dateByAddingComponents:dayComponent toDate:currentDate options:0];

            NSString *dateString = [dateFormatter stringFromDate:date];

            NSLog(@"date = %@", dateString);
        }

我希望所有这些都会有所帮助.

I hope all this helps.

这篇关于iOS 和 Objective-C:每季度重复一次事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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