IOS年、月、日三种日期加减选择器
来源:互联网 发布:手机直播完整源码 编辑:程序博客网 时间:2024/05/22 15:37
今天和大家分享一下,我自己写的关于日期分别已年、月、日加减的方法,话不多说直接贴代码:
#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
- IOS年、月、日三种日期加减选择器
- IOS加减选择器
- iOS NSDate日期加减
- iOS NSDate日期加减
- iOS NSDate日期加减
- iOS日期加减计算
- javascript日期加减——年、月、日
- Android 日期选择器隐藏年,月,日
- SQL 日期计算 加减3个月
- iOS添加日期选择器
- IOS 学习 日期选择器
- 仿ios日期选择器
- iOS 自定义日期选择器
- iOS 自定义日期选择器
- 【iOS】自定义日期选择器
- iOS日期选择器
- iOS日期选择器
- 2017年2月28日手记---bootstrap日期选择器
- BS版代码生成器 简介
- spring bean初始化
- 央企、创业企业、金融大企业的对比
- SPDY、HTTP/2、QUIC协议
- ExecutorService——newScheduledThreadPool使用功能测试
- IOS年、月、日三种日期加减选择器
- PullUtils解析xml
- " "和null的区别
- 动态代理和静态代理
- 循环获取eq选择器的值
- mybatis的简单使用
- Maven 手动添加 JAR 包到本地仓库
- zeroc-ice网络请求使用介绍
- JAVA中重写equals()方法的同时要重写hashcode()方法