IOS年、月、日三种日期加减选择器

今天和大家分享一下,我自己写的关于日期分别已年、月、日加减的方法,话不多说直接贴代码:

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic , weak)UILabel *dataLable;

@property (nonatomic , copy)NSDate *currentDate;

@property (nonatomic , assign) int type;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.type = 2;

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 50)];
    view.backgroundColor = [UIColor grayColor];
    [self.view addSubview:view];

    UIButton *leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
    [leftBtn setTitle:@"+" forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(leftBtn) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:leftBtn];


    UILabel *datalable = [[UILabel alloc] initWithFrame:CGRectMake(50, 0, 100, 50)];
    self.dataLable = datalable;
    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc]init];
    NSDate *nowDate = [NSDate date];
    self.currentDate = nowDate;
    if (self.type == 0) {
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        self.dataLable.text  = [NSString stringWithFormat:@"%@",[dateFormatter stringFromDate:nowDate]];
    } else if (self.type == 1) {
        [dateFormatter setDateFormat:@"yyyy-MM"];
        self.dataLable.text  = [NSString stringWithFormat:@"%@",[dateFormatter stringFromDate:nowDate]];
    } else if (self.type == 3) {
        [dateFormatter setDateFormat:@"yyyy年"];
        self.dataLable.text  = [NSString stringWithFormat:@"%@",[dateFormatter stringFromDate:nowDate]];
    } else {
        [dateFormatter setDateFormat:@"yyyy-MM"];
        NSArray *dateArray = [[dateFormatter stringFromDate:nowDate] componentsSeparatedByString:@"-"];
        CGFloat i = [dateArray[1] floatValue] / 3;
        NSString *str = [NSString string];
        if (i <= 1) {
            str = @"一";
        } else if (i >1 && i <= 2) {
            str = @"二";
        } else if (i >2 && i <= 3) {
            str = @"三";
        } else {
            str = @"四";
        }
        self.dataLable.text  = [NSString stringWithFormat:@"%@年第%@季度", dateArray[0], str];
    }
    [view addSubview:datalable];

    UIButton *rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(150, 0, 50, 50)];
    [rightBtn setTitle:@"-" forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(rightBtn) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:rightBtn];
}

- (void)leftBtn {
    if (self.type == 0) {
        self.dataLable.text = [self getDate:0 month:0 day:1];
    } else if (self.type == 1) {
        self.dataLable.text = [self getDate:0 month:1 day:0];
    } else if (self.type == 3) {
        self.dataLable.text = [self getDate:1 month:0 day:0];
    } else {
        self.dataLable.text = [self getDate:0 month:3 day:0];
    }

}

- (void)rightBtn {
    if (self.type == 0) {
        self.dataLable.text = [self getDate:0 month:0 day:-1];
    } else if (self.type == 1) {
        self.dataLable.text = [self getDate:0 month:-1 day:0];
    } else if (self.type == 3) {
        self.dataLable.text = [self getDate:-1 month:0 day:0];
    } else {
        self.dataLable.text = [self getDate:0 month:-3 day:0];
    }
}

- (NSString*)getDate:(int)year month:(int)month day:(int)day{

    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

    NSDateComponents *comps = nil;

    comps = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:[NSDate date]];

    NSDateComponents *adcomps = [[NSDateComponents alloc] init];

    [adcomps setYear:year];

    [adcomps setMonth:month];

    [adcomps setDay:day];

    NSDate *newdate = [calendar dateByAddingComponents:adcomps toDate:self.currentDate options:0];

    self.currentDate = newdate;

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

    if (self.type == 0) {
        [formatter setDateFormat:@"yyyy-MM-dd"];
    } else if (self.type == 1) {
        [formatter setDateFormat:@"yyyy-MM"];
    } else if (self.type == 3) {
        [formatter setDateFormat:@"yyyy年"];
    } else {
        [formatter setDateFormat:@"yyyy-MM"];
    }

    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/beijing"];

    [formatter setTimeZone:timeZone];

    NSString *dateFromData = [formatter stringFromDate:newdate];

    if (self.type == 2) {
        NSArray *dateArray = [dateFromData componentsSeparatedByString:@"-"];
        CGFloat i = [dateArray[1] floatValue] / 3;
        NSString *str = [NSString string];
        if (i <= 1) {
            str = @"一";
        } else if (i >1 && i <= 2) {
            str = @"二";
        } else if (i >2 && i <= 3) {
            str = @"三";
        } else {
            str = @"四";
        }
        dateFromData  = [NSString stringWithFormat:@"%@年第%@季度", dateArray[0], str];
    }
    return dateFromData;

}

可以通过type的值来改变日期label的显示,加减按钮分别可以加减日期。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值