今天和大家分享一下,我自己写的关于日期分别已年、月、日加减的方法,话不多说直接贴代码:
#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的显示,加减按钮分别可以加减日期。