image.png
查询筛选条件 支持 年 季度 月 旬 周 日选择器
首先创建一个NSDate类别 处理日期的数据 .h文件
#pragma mark - 日期格式化处理
/**
* 将对应格式的时间字符串转化成时间对象
*
* @param dateString 时间字符串
* @param formatterString 格式化字符串
*
* @return 格式化之后的时间
*/
+ (NSDate *)dateWithString:(NSString *)dateString
formatterString:(NSString *)formatterString;
/// date对象转化为字符串 @"MM-dd"
/// @param formatterString 转换格式
- (NSString *)dateFormatterString:(NSString *)formatterString;
#pragma mark - 日历相关
/** 当前月有几天 */
- (NSUInteger)numberOfDaysInCurrentMonth;
/** 当前月有几周 */
- (NSUInteger)numberOfWeeksInCurrentMonth;
/** 获取当前日期是星期几
1,2, 3, 4,5, 6,7
日,一,二,三,四,五,六*/
- (NSInteger)weekday;
/** 获取当前日期是星期几
1,2, 3, 4,5, 6,7
日,一,二,三,四,五,六*/
- (NSUInteger)weeklyOrdinality;
/** 计算这个月最开始的一天 */
- (NSDate *)firstDayOfCurrentMonth;
/** 计算这个月最后一天 */
- (NSDate *)lastDayOfCurrentMonth;
/** 获取年月日对象 */
- (NSDateComponents *)YMDComponents;
/** 距离当前日期前后几天的日期 */
- (NSDate *)beforeAfterDays:(NSInteger)days;
/** 距离当前日期前后几个月的日期 */
- (NSDate *)beforeAfterMonths:(NSInteger)months;
/** 距离当前日期前后几年的日期 */
- (NSDate *)beforeAfterYears:(NSInteger)years;
/** 计算两个日期之间相差几天(包含当天)如:05-11~05-12(2天) */
- (NSInteger)deltaDaysToDate:(NSDate *)anotherDate;
/// 本月第一天日期
- (NSDate *)firstDateOfMonth;
/// 本月第一天周几
- (NSUInteger)startDayOfWeek;
/// 获取上个月日期
- (NSDate *)getLastMonth;
/// 获取下个月日期
- (NSDate *)getNextMonth;
/// 本月最后一天周几
- (NSUInteger)lastDayOfWeek;
/// 根据日号获取日期
/// @param day 日号
- (NSDate *)dateOfDay:(NSInteger)day;
//Other 日期格式化字符串输出
- (NSString *)mmddByLineWithDate;
- (NSString *)yyyyMMByLineWithDate;
- (NSString *)yyyyMMddByLineWithDate;
- (NSString *)mmddChineseWithDate;
- (NSString *)hhmmssWithDate;
/// yyyy-MM 格式字符串 时间转化
- (NSString *)yyyyMMBySeparatorLineWithDate;
/// 获得某年的周数 有多少自然周
/// @param year 年数
+ (NSInteger)getWeekNumberOfYear:(NSInteger)year;
/// 判断日期属于第几自然周
/// @param date 日期
+ (NSInteger)getWeekNumberOfYearWithDate:(NSDate *)date;
/// 获取每周开始时间
/// @param i 每年的第几周
+ (NSDate *)getWeekStartDateWithWeekNumder:(NSInteger)i;
/// 每周最后一天日期
/// @param i 一年的第几周
+ (NSDate *)getWeekEndDateWithWeekNumder:(NSInteger)i;
/**
* 获取某年某周的范围日期 开始日期
*
* @param year 年份
* @param weekofYear year里某个周
*
* @return 时间范围字符串
*/
+ (NSDate *)getWeekRangeStartDate_Year:(NSInteger)year WeakOfYear:(NSInteger)weekofYear;
/**
* 获取某年某周的范围日期 结束日期
*
* @param year 年份
* @param weekofYear year里某个周
*
* @return 时间范围字符串
*/
+ (NSDate *)getWeekRangeEndDate_Year:(NSInteger)year WeakOfYear:(NSInteger)weekofYear;
/**
*获取系统时间戳
@return 时间戳
*/
+ (NSTimeInterval)wt_getCurentTimeStamp;
/** 时间戳转时间 */
+ (NSString *)getNowTimestamp:(long long)timerNumber dateFormat:(NSString *)dateFormat;
.m文件
#pragma mark - 日期格式化处理
+ (NSDate *)dateWithString:(NSString *)dateString
formatterString:(NSString *)formatterString {
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:formatterString];
return [formatter dateFromString:dateString];
}
/// date对象转化为字符串 @"MM-dd"
/// @param formatterString 转换格式
- (NSString *)dateFormatterString:(NSString *)formatterString {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
if ([WTTool isNilWithString:formatterString]) {
formatterString = @"yyyy-MM-dd";
}
[formatter setDateFormat:formatterString];
return [formatter stringFromDate:self];
}
#pragma mark - 日历相关
/** 当前月有几天 */
- (NSUInteger)numberOfDaysInCurrentMonth {
return [[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:self].length;
}
/** 当前月有几周 */
- (NSUInteger)numberOfWeeksInCurrentMonth {
NSUInteger weekday = [[self firstDayOfCurrentMonth] weeklyOrdinality];
NSUInteger days = [self numberOfDaysInCurrentMonth];
NSUInteger weeks = 0;
if (weekday > 1) {
(void)(weeks += 1), days -= (7 - weekday + 1);
}
weeks += days / 7;
weeks += (days % 7 > 0) ? 1 : 0;
return weeks;
}
/** 获取当前日期是星期几
1,2, 3, 4,5, 6,7
日,一,二,三,四,五,六*/
- (NSUInteger)weeklyOrdinality {
return [[NSCalendar currentCalendar] ordinalityOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitWeekOfYear forDate:self];
}
/** 获取当前日期是星期几
1,2, 3, 4,5, 6,7
日,一,二,三,四,五,六*/
- (NSInteger)weekday {
NSCalendar *calender = [NSCalendar currentCalendar];
NSDateComponents *componets = [calender components:NSCalendarUnitWeekday fromDate:self];
return [componets weekday];
}
/** 计算这个月最开始的一天 */
- (NSDate *)firstDayOfCurrentMonth {
NSDate *startDate = nil;
[[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitMonth startDate:&startDate interval:NULL forDate:self];
// NSAssert1(ok, @"Failed to calculate the first day of the month based on %@", self);
return startDate;
}
/** 计算这个月最后一天 */
- (NSDate *)lastDayOfCurrentMonth {
NSCalendarUnit calendarUnit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:calendarUnit fromDate:self];
dateComponents.day = [self numberOfDaysInCurrentMonth];
return [[NSCalendar currentCalendar] dateFromComponents:dateComponents];
}
/// 本月第一天日期
- (NSDate *)firstDateOfMonth {
NSCalendar *greCalendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
[greCalendar setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
NSDateComponents *comps = [greCalendar
components:NSCalendarUnitYear | NSCalendarUnitMonth |NSCalendarUnitWeekday | NSCalendarUnitDay
fromDate:self];
comps.day = 1;
return [greCalendar dateFromComponents:comps];
}
/// 本月第一天周几
- (NSUInteger)startDayOfWeek {
NSCalendar *greCalendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
[greCalendar setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];//Asia/Shanghai
NSDateComponents *comps = [greCalendar
components:NSCalendarUnitYear | NSCalendarUnitMonth |NSCalendarUnitWeekday | NSCalendarUnitDay
fromDate:[self firstDateOfMonth]];
return comps.weekday;
}
/// 获取上个月日期
- (NSDate *)getLastMonth {
NSCalendar *greCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
[greCalendar setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
NSDateComponents *comps = [greCalendar
components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay
fromDate:self];
comps.month -= 1;
return [greCalendar dateFromComponents:comps];
}
/// 获取下个月日期
- (NSDate *)getNextMonth {
NSCalendar *greCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
[greCalendar setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
NSDateComponents *comps = [greCalendar
components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay
fromDate:self];
comps.month += 1;
return [greCalendar dateFromComponents:comps];
}
/// 本月最后一天周几
- (NSUInteger)lastDayOfWeek {
NSCalendar *greCalendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
[greCalendar setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];//Asia/Shanghai
NSDateComponents *comps = [greCalendar
components:NSCalendarUnitYear | NSCalendarUnitMonth |NSCalendarUnitWeekday | NSCalendarUnitDay
fromDate:self];
return comps.weekday;
}
/// 根据日号获取日期
/// @param day 日号
- (NSDate *)dateOfDay:(NSInteger)day {
NSCalendar *greCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
[greCalendar setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
NSDateComponents *comps = [greCalendar
components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay
fromDate:self];
comps.day = day;
return [greCalendar dateFromComponents:comps];
}
/** 获取年月日对象 */
- (NSDateComponents *)YMDComponents {
return [[NSCalendar currentCalendar] components:
NSCalendarUnitYear|
NSCalendarUnitMonth|
NSCalendarUnitDay|
NSCalendarUnitWeekday fromDate:self];
}
/** 距离当前日期前后几天的日期 */
- (NSDate *)beforeAfterDays:(NSInteger)days {
NSDateComponents *components = [[NSDateComponents alloc]init];
components.day = days;
return [[NSCalendar currentCalendar]dateByAddingComponents:components toDate:self options:0];
}
/** 距离当前日期前后几个月的日期 */
- (NSDate *)beforeAfterMonths:(NSInteger)months {
NSDateComponents *components = [[NSDateComponents alloc]init];
components.month = months;
return [[NSCalendar currentCalendar]dateByAddingComponents:components toDate:self options:0];
}
/** 距离当前日期前后几年的日期 */
- (NSDate *)beforeAfterYears:(NSInteger)years {
NSDateComponents *components = [[NSDateComponents alloc]init];
components.year = years;
return [[NSCalendar currentCalendar]dateByAddingComponents:components toDate:self options:0];
}
/** 计算两个日期之间相差几天(包含当天)如:05-11~05-12(2天) */
- (NSInteger)deltaDaysToDate:(NSDate *)anotherDate {
NSTimeInterval timeInterval = [self timeIntervalSinceDate:anotherDate];
NSTimeInterval one_day = 60*60*24;
NSInteger deltaDays = (NSInteger)(timeInterval/one_day);
return deltaDays;
}
- (NSString *)yyyyMMByLineWithDate {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy年MM月"];
return [formatter stringFromDate:self];
}
- (NSString *)mmddByLineWithDate {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MM-dd"];
return [formatter stringFromDate:self];
}
- (NSString *)mmddChineseWithDate {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MM月dd日"];
return [formatter stringFromDate:self];
}
- (NSString *)hhmmssWithDate {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.timeZone = [NSTimeZone timeZoneWithName:@"GMT"];
[formatter setDateFormat:@"HH:mm:ss"];
return [formatter stringFromDate:self];
}
- (NSString *)yyyyMMddByLineWithDate {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd"];
return [formatter stringFromDate:self];
}
/// yyyy-MM 格式字符串 时间转化
- (NSString *)yyyyMMBySeparatorLineWithDate {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM"];
return [formatter stringFromDate:self];
}
/// 获得某年的周数 有多少自然周
/// @param year 年数
+ (NSInteger)getWeekNumberOfYear:(NSInteger)year {
NSCalendar *greCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
[greCalendar setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
NSDate *date = [NSDate dateWithString:[NSString stringWithFormat:@"%ld-12-31",year] formatterString:@"yyyy-MM-dd"];
NSDateComponents *comps = [greCalendar
components:NSCalendarUnitYear | NSCalendarUnitMonth |NSCalendarUnitWeekday | NSCalendarUnitDay | NSCalendarUnitWeekOfYear
fromDate:date];
NSInteger week = [comps weekOfYear];
if (week == 1) {
return 52;
}else {
return week;
}
}
/// 判断日期属于第几自然周
/// @param date 日期
+ (NSInteger)getWeekNumberOfYearWithDate:(NSDate *)date {
NSCalendar *greCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
[greCalendar setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
NSDateComponents *comps = [greCalendar
components:NSCalendarUnitYear | NSCalendarUnitMonth |NSCalendarUnitWeekday | NSCalendarUnitDay | NSCalendarUnitWeekOfYear
fromDate:date];
NSInteger week = [comps weekOfYear];
return week;
}
/// 获取每周开始时间
/// @param i 每年的第几周
+ (NSDate *)getWeekStartDateWithWeekNumder:(NSInteger)i {
NSDate *nowDate = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comp = [calendar components: NSCalendarUnitWeekday | NSCalendarUnitWeekOfYear fromDate:nowDate];
// 获取今天是周几 (周一是2,周日是1)
NSInteger weekDay = [comp weekday];
//这周是今年的第几周(注意:第一周编号为1不为0)
NSInteger weekOfYear = [comp weekOfYear];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyyMMdd"];
NSDate *date = [formatter dateFromString:[formatter stringFromDate:[NSDate date]]];
//这周的开始和结束时间 (注意:+1 -1 避开临界值)
NSDate *startdate = [date dateByAddingTimeInterval:24*3600*(2 - weekDay)+1];
//一年52周 获取一年的所有周一和周日的日期(注意:第一周编号为1不为0)
NSDate *startdate1 = [startdate dateByAddingTimeInterval:24*3600*7*(i-weekOfYear)];
return startdate1;
}
/// 每周最后一天日期
/// @param i 一年的第几周
+ (NSDate *)getWeekEndDateWithWeekNumder:(NSInteger)i {
NSDate *nowDate = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comp = [calendar components: NSCalendarUnitWeekday | NSCalendarUnitWeekOfYear fromDate:nowDate];
// 获取今天是周几 (周一是2,周日是1)
NSInteger weekDay = [comp weekday];
//这周是今年的第几周(注意:第一周编号为1不为0)
NSInteger weekOfYear = [comp weekOfYear];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyyMMdd"];
NSDate *date = [formatter dateFromString:[formatter stringFromDate:[NSDate date]]];
//这周的开始和结束时间 (注意:+1 -1 避开临界值)
NSDate *enddate = [date dateByAddingTimeInterval:24*3600*(9 - weekDay)-1];
//一年52周 获取一年的所有周一和周日的日期(注意:第一周编号为1不为0)
NSDate *enddate1 = [enddate dateByAddingTimeInterval:24*3600*7*(i-weekOfYear)];
return enddate1;
}
- (NSArray *)getAllWeekDateWithYear:(NSInteger)year {
NSDate *nowDate = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comp = [calendar components: NSCalendarUnitWeekday | NSCalendarUnitWeekOfYear fromDate:nowDate];
// 获取今天是周几 (周一是2,周日是1)
NSInteger weekDay = [comp weekday];
//这周是今年的第几周(注意:第一周编号为1不为0)
NSInteger weekOfYear = [comp weekOfYear];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyyMMdd"];
NSDate *date = [formatter dateFromString:[formatter stringFromDate:[NSDate date]]];
//这周的开始和结束时间 (注意:+1 -1 避开临界值)
NSDate *startdate = [date dateByAddingTimeInterval:24*3600*(2 - weekDay)+1];
NSDate *enddate = [date dateByAddingTimeInterval:24*3600*(9 - weekDay)-1];
NSMutableArray *dateArray = [[[NSMutableArray alloc] init] mutableCopy];
//一年52周 获取一年的所有周一和周日的日期(注意:第一周编号为1不为0)
for (int i = 1; i <= 52; i++) {
NSDate *startdate1 = [startdate dateByAddingTimeInterval:24*3600*7*(i-weekOfYear)];
NSDate *enddate1 = [enddate dateByAddingTimeInterval:24*3600*7*(i-weekOfYear)];
[dateArray addObject:@[startdate1,enddate1]];
}
return dateArray;
}
/**
* 获取某年某周的范围日期 开始日期
*
* @param year 年份
* @param weekofYear year里某个周
*
* @return 时间范围字符串
*/
+ (NSDate *)getWeekRangeStartDate_Year:(NSInteger)year WeakOfYear:(NSInteger)weekofYear {
NSString *timeAxis = [NSString stringWithFormat:@"%ld-06-01 12:00:00",(long)year];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:timeAxis];
NSCalendar *calendar = [NSCalendar currentCalendar];
/**这两个参数的设置影响着周次的个数和划分*****************/
[calendar setFirstWeekday:2]; //设置每周的开始是星期一
// [calendar setMinimumDaysInFirstWeek:7]; //设置一周至少需要几天
NSDateComponents *comps = [calendar components:(NSCalendarUnitWeekOfYear | NSCalendarUnitWeekday | NSCalendarUnitWeekdayOrdinal | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay)
fromDate:date];
//时间轴是当前年的第几周
NSInteger todayIsWeek = [comps weekOfYear];
if ([NSDate getWeekNumberOfYear:year] == 53) {
todayIsWeek += 1;
}
//获取时间轴是星期几 1(星期天) 2(星期一) 3(星期二) 4(星期三) 5(星期四) 6(星期五) 7(星期六)
NSInteger todayIsWeekDay = [comps weekday];
// 计算当前日期和这周的星期一和星期天差的天数
//firstDiff 星期一相差天数 、 lastDiff 星期天相差天数
long firstDiff;
if (todayIsWeekDay == 1) {
firstDiff = -6;
// lastDiff = 0;
} else {
firstDiff = [calendar firstWeekday] - todayIsWeekDay;
// lastDiff = 8 - todayIsWeekDay;
}
NSDate *firstDayOfWeek= [NSDate dateWithTimeInterval:24*60*60*firstDiff sinceDate:date];
long weekdifference = weekofYear - todayIsWeek;
firstDayOfWeek= [NSDate dateWithTimeInterval:24*60*60*7*weekdifference sinceDate:firstDayOfWeek];
return firstDayOfWeek;
}
/**
* 获取某年某周的范围日期 结束日期
*
* @param year 年份
* @param weekofYear year里某个周
*
* @return 时间范围字符串
*/
+ (NSDate *)getWeekRangeEndDate_Year:(NSInteger)year WeakOfYear:(NSInteger)weekofYear {
NSString *timeAxis = [NSString stringWithFormat:@"%ld-06-01 12:00:00",(long)year];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:timeAxis];
NSCalendar *calendar = [NSCalendar currentCalendar];
/**这两个参数的设置影响着周次的个数和划分*****************/
[calendar setFirstWeekday:2]; //设置每周的开始是星期一
// [calendar setMinimumDaysInFirstWeek:7]; //设置一周至少需要几天
NSDateComponents *comps = [calendar components:(NSCalendarUnitWeekOfYear | NSCalendarUnitWeekday | NSCalendarUnitWeekdayOrdinal | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay)
fromDate:date];
//时间轴是当前年的第几周
NSInteger todayIsWeek = [comps weekOfYear];
if ([NSDate getWeekNumberOfYear:year] == 53) {
todayIsWeek += 1;
}
//获取时间轴是星期几 1(星期天) 2(星期一) 3(星期二) 4(星期三) 5(星期四) 6(星期五) 7(星期六)
NSInteger todayIsWeekDay = [comps weekday];
// 计算当前日期和这周的星期一和星期天差的天数
//firstDiff 星期一相差天数 、 lastDiff 星期天相差天数
long lastDiff;
if (todayIsWeekDay == 1) {
// firstDiff = -6;
lastDiff = 0;
} else {
// firstDiff = [calendar firstWeekday] - todayIsWeekDay;
lastDiff = 8 - todayIsWeekDay;
}
NSDate *lastDayOfWeek= [NSDate dateWithTimeInterval:24*60*60*lastDiff sinceDate:date];
long weekdifference = weekofYear - todayIsWeek;
lastDayOfWeek= [NSDate dateWithTimeInterval:24*60*60*7*weekdifference sinceDate:lastDayOfWeek];
return lastDayOfWeek;
}
/**
*获取系统时间戳
@return 时间戳
*/
+ (NSTimeInterval)wt_getCurentTimeStamp {
NSDate *datenow = [NSDate date];//现在时间,你可以输出来看下是什么格式
NSTimeInterval interTime = [datenow timeIntervalSince1970] * 1000;
return interTime;
}
/** 时间戳转时间 */
+ (NSString *)getNowTimestamp:(long long)timerNumber dateFormat:(NSString *)dateFormat {
NSTimeInterval interval = timerNumber / 1000.0;
NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:dateFormat];
return [formatter stringFromDate:date];
}
接下来 日期服务工具 DateService.h
/** 日期选择器类型 */
typedef NS_ENUM(NSUInteger, DatePickerType) {
/** 日选择 */
DatePickerTypeDay = 0,
/** 周选择 */
DatePickerTypeWeek = 1,
/** 旬选择 */
DatePickerTypeMeadow = 2,
/** 月选择 */
DatePickerTypeMonth = 3,
/** 季选择 */
DatePickerTypeSeason = 4,
/** 年选择 */
DatePickerTypeYear = 5,
};
/** 日期服务工具 */
@interface DateService : NSObject
/** 默认日期 */
@property (strong, nonatomic) NSDate *defalutDate;
/// 日期类型
@property (nonatomic,assign)DatePickerType dateType;
/// 第一列索引
@property (nonatomic,assign)NSInteger deFirstRow;
/// 第二列索引
@property (nonatomic,assign)NSInteger deSecondRow;
/// 第三列索引
@property (nonatomic,assign)NSInteger deThirdRow;
#pragma mark - 创建单例
+ (instancetype)shareManager;
/// 获取开始日期
- (NSDate *)startDateWithDate;
/// 获取结束日期
- (NSDate *)endDateWithDate;
/// 显示的日期字符串
- (NSString *)showDateString;
/// 向后退一个日期
- (void)backwardDate;
/// 向右一个日期 向前
- (void)forwardDate;
@end
DateService.m
@interface DateService ()
/** 最小日期 */
@property (strong, nonatomic) NSDate *miniDate;
/** 最大日期 */
@property (strong, nonatomic) NSDate *maxDate;
/** 数据源数组*/
@property (strong, nonatomic) NSMutableArray *dataSourceArr;
@end
static DateService *manager = nil;
@implementation DateService
#pragma mark - 创建单例
+ (instancetype)shareManager {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
manager = [[self alloc] init];
});
manager.miniDate = [NSDate dateWithString:@"2020-01-01" formatterString:@"yyyy-MM-dd"];
manager.maxDate = [NSDate date];
manager.defalutDate = [NSDate date];
return manager;
}
+ (id)allocWithZone:(struct _NSZone *)zone {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
manager = [super allocWithZone:zone];
});
return manager;
}
- (nonnull id)copyWithZone:(nullable NSZone *)zone {
return manager;
}
- (nonnull id)mutableCopyWithZone:(nullable NSZone *)zone {
return manager;
}
- (void)setDateType:(DatePickerType)dateType {
_dateType = dateType;
[self.dataSourceArr removeAllObjects];
if (dateType == DatePickerTypeDay) {
[self dayTypeModel];
} else if (dateType == DatePickerTypeMonth) {
[self monthTypeSource];
} else if (dateType == DatePickerTypeYear) {
[self yearTypeSource];
} else if (dateType == DatePickerTypeSeason) {
[self seasonTypeSource];
} else if (dateType == DatePickerTypeMeadow) {
[self meadowTypeModel];
} else if (dateType == DatePickerTypeWeek) {
[self weekDataSource];
}
}
/// 获取开始日期
- (NSDate *)startDateWithDate {
NSDate *dat = nil;
if (self.dateType == DatePickerTypeDay) {
dat = [self dayStatDate];
} else if (self.dateType == DatePickerTypeWeek) {
dat = [self weekDateIsStart:YES];
} else if (self.dateType == DatePickerTypeMeadow) {//旬
dat = [self medowStartDate];
} else if (self.dateType == DatePickerTypeMonth) {//月
dat = [self monthStartDateWithIsStart:YES];
} else if (self.dateType == DatePickerTypeSeason) {//季度
dat = [self seasonDateStartIs:YES];
} else { //年
dat = [self yearDateIsStart:YES];
}
return dat;
}
/// 获取结束日期
- (NSDate *)endDateWithDate {
NSDate *dat = nil;
if (self.dateType == DatePickerTypeDay) {
dat = [self dayStatDate];
} else if (self.dateType == DatePickerTypeWeek) {
dat = [self weekDateIsStart:NO];
} else if (self.dateType == DatePickerTypeMeadow) { //旬
dat = [self medowEndsDate];
} else if (self.dateType == DatePickerTypeMonth) {//月
dat = [self monthStartDateWithIsStart:NO];
} else if (self.dateType == DatePickerTypeSeason) {//季度
dat = [self seasonDateStartIs:NO];
} else { //年
dat = [self yearDateIsStart:NO];
}
return dat;
}
/// 日类型数据返回
- (NSDate *)dayStatDate {
NSDate *date = nil;
JXDateFirstModel *fModel = self.dataSourceArr[self.deFirstRow];
JXDateSecondModel *sModel = fModel.secondArray[self.deSecondRow];
JXDateThirdModel *tModel = sModel.thirdArray[self.deThirdRow];
NSString *dayStr = [NSString stringWithFormat:@"%@-%@-%@",fModel.name,sModel.name,tModel.name];
date = [NSDate dateWithString:dayStr formatterString:@"yyyy-MM-dd"];
return date;
}
/// 获取周的开始 结束日期
/// @param isStart 是否是开始
- (NSDate *)weekDateIsStart:(BOOL)isStart {
NSDate *weekD = nil;
JXDateFirstModel *fModel = self.dataSourceArr[self.deFirstRow];
NSInteger year = [fModel.name integerValue];
JXDateSecondModel *sModel = fModel.secondArray[self.deSecondRow];
NSInteger weekn = sModel.weekNumber;
if (isStart) {
weekD = [NSDate getWeekRangeStartDate_Year:year WeakOfYear:weekn];
} else {
weekD = [NSDate getWeekRangeEndDate_Year:year WeakOfYear:weekn];
}
return weekD;
}
/// 旬开始日期
- (NSDate *)medowStartDate {
NSDate *meadow = nil;
JXDateFirstModel *fModel = self.dataSourceArr[self.deFirstRow];
NSInteger year = [fModel.name integerValue];
JXDateSecondModel *sModel = fModel.secondArray[self.deSecondRow];
NSInteger mont = [sModel.name integerValue];
JXDateThirdModel *tModel = sModel.thirdArray[self.deThirdRow];
NSInteger meadowN = tModel.meadow;
NSInteger day = 1;
if (meadowN == 1) {
day = 1;
} else if (meadowN == 2) {
day = 11;
} else {
day = 21;
}
meadow = [NSDate dateWithString:[NSString stringWithFormat:@"%ld-%ld-%ld",(long)year,(long)mont,(long)day] formatterString:@"yyyy-MM-dd"];
return meadow;
}
/// 旬结束日期
- (NSDate *)medowEndsDate {
NSDate *meadow = nil;
JXDateFirstModel *fModel = self.dataSourceArr[self.deFirstRow];
NSInteger year = [fModel.name integerValue];
JXDateSecondModel *sModel = fModel.secondArray[self.deSecondRow];
NSInteger mont = [sModel.name integerValue];
JXDateThirdModel *tModel = sModel.thirdArray[self.deThirdRow];
NSInteger meadowN = tModel.meadow;
NSInteger day = 1;
if (meadowN == 1) {
day = 10;
} else if (meadowN == 2) {
day = 20;
} else {
NSDate *motnDate = [NSDate dateWithString:[NSString stringWithFormat:@"%ld-%ld",year,(long)mont] formatterString:@"yyyy-MM"];
day = [motnDate numberOfDaysInCurrentMonth];
}
meadow = [NSDate dateWithString:[NSString stringWithFormat:@"%ld-%ld-%ld",(long)year,(long)mont,(long)day] formatterString:@"yyyy-MM-dd"];
return meadow;
}
/// 获取月的开始和结束日期
/// @param isStart 是否开始日期
- (NSDate *)monthStartDateWithIsStart:(BOOL)isStart {
NSDate *mDate = nil;
JXDateFirstModel *fModel = self.dataSourceArr[self.deFirstRow];
NSInteger year = [fModel.name integerValue];
JXDateSecondModel *sModel = fModel.secondArray[self.deSecondRow];
NSInteger mon = [sModel.name integerValue];
NSInteger day = 1;
if (isStart) {
day = 1;
} else {
NSDate *motnDate = [NSDate dateWithString:[NSString stringWithFormat:@"%ld-%ld",year,(long)mon] formatterString:@"yyyy-MM"];
day = [motnDate numberOfDaysInCurrentMonth];
}
mDate = [NSDate dateWithString:[NSString stringWithFormat:@"%ld-%ld-%ld",(long)year,(long)mon,(long)day] formatterString:@"yyyy-MM-dd"];
return mDate;
}
/// 季度开始结束日期
/// @param isStart 是否是开始日期
- (NSDate *)seasonDateStartIs:(BOOL)isStart {
NSDate *seasonDate = nil;
JXDateFirstModel *fModel = self.dataSourceArr[self.deFirstRow];
NSInteger year = [fModel.name integerValue];
JXDateSecondModel *sModel = fModel.secondArray[self.deSecondRow];
NSInteger seasonN = [sModel.name integerValue];
NSInteger day = 1;
NSInteger month = 1;
if (isStart) {
if (seasonN == 1) {
month = 1;
} else if (seasonN == 2) {
month = 4;
} else if (seasonN == 3) {
month = 7;
} else {
month = 10;
}
} else {
if (seasonN == 1) {
month = 3;
day = 31;
} else if (seasonN == 2) {
month = 6;
day = 30;
} else if (seasonN == 3) {
month = 9;
day = 30;
} else {
month = 12;
day = 31;
}
}
seasonDate = [NSDate dateWithString:[NSString stringWithFormat:@"%ld-%ld-%ld",(long)year,(long)month,(long)day] formatterString:@"yyyy-MM-dd"];
return seasonDate;
}
/// 年开始结束日期
/// @param isStart 是否开始
- (NSDate *)yearDateIsStart:(BOOL)isStart {
NSDate *yDate = nil;
JXDateFirstModel *fModel = self.dataSourceArr[self.deFirstRow];
NSInteger year = [fModel.name integerValue];
NSInteger day = 1;
NSInteger month = 1;
if (isStart) {
month = 1;
day = 1;
} else {
month = 12;
day = 31;
}
yDate = [NSDate dateWithString:[NSString stringWithFormat:@"%ld-%ld-%ld",(long)year,(long)month,(long)day] formatterString:@"yyyy-MM-dd"];
return yDate;
}
/// 显示的日期字符串
- (NSString *)showDateString {
NSString *dateStr = @"";
if (self.dateType == DatePickerTypeDay) {
dateStr = [self dayStr];
} else if (self.dateType == DatePickerTypeWeek) {
dateStr = [self weekShowStr];
} else if (self.dateType == DatePickerTypeMeadow) { //旬
dateStr = [self meadowShowStr];
} else if (self.dateType == DatePickerTypeMonth) {//月
dateStr = [self monthStr];
} else if (self.dateType == DatePickerTypeSeason) {//季度
dateStr = [self seasonShowStr];
} else { //年
dateStr = [self yearShowStr];
}
return dateStr;
}
/// 日 显示字符串
- (NSString *)dayStr {
NSString *dayStr = @"";
JXDateFirstModel *fModel = self.dataSourceArr[self.deFirstRow];
JXDateSecondModel *sModel = fModel.secondArray[self.deSecondRow];
JXDateThirdModel *tModel = sModel.thirdArray[self.deThirdRow];
dayStr = [NSString stringWithFormat:@"%@/%@/%@",fModel.name,sModel.name,tModel.name];
return dayStr;
}
/// 周 显示字符串
- (NSString *)weekShowStr {
NSString *weekStr = @"";
JXDateFirstModel *fModel = self.dataSourceArr[self.deFirstRow];
NSInteger year = [fModel.name integerValue];
JXDateSecondModel *sModel = fModel.secondArray[self.deSecondRow];
NSInteger weekn = sModel.weekNumber;
NSDate *star = [NSDate getWeekRangeStartDate_Year:year WeakOfYear:weekn];
NSDate *endDay = [NSDate getWeekRangeEndDate_Year:year WeakOfYear:weekn];
weekStr = [NSString stringWithFormat:@"%@-%@",[star dateFormatterString:@"yyyy/MM/dd"],[endDay dateFormatterString:@"yyyy/MM/dd"]];
return weekStr;
}
/// 旬显示 字符传
- (NSString *)meadowShowStr {
NSString *meadowStr = @"";
JXDateFirstModel *fModel = self.dataSourceArr[self.deFirstRow];
JXDateSecondModel *sModel = fModel.secondArray[self.deSecondRow];
JXDateThirdModel *tModel = sModel.thirdArray[self.deThirdRow];
meadowStr = [NSString stringWithFormat:@"%@/%@/%@",fModel.name,sModel.name,tModel.name];
return meadowStr;
}
/// 月份展示
- (NSString *)monthStr {
NSString *monthStr = @"";
JXDateFirstModel *fModel = self.dataSourceArr[self.deFirstRow];
JXDateSecondModel *sModel = fModel.secondArray[self.deSecondRow];
monthStr = [NSString stringWithFormat:@"%@/%@",fModel.name,sModel.name];
return monthStr;
}
/// 季度显示字符串
- (NSString *)seasonShowStr {
NSString *saesonStr = @"";
JXDateFirstModel *fModel = self.dataSourceArr[self.deFirstRow];
JXDateSecondModel *sModel = fModel.secondArray[self.deSecondRow];
saesonStr = [NSString stringWithFormat:@"%@ %@",fModel.name,sModel.title];
return saesonStr;
}
/// 年 显示字符串
- (NSString *)yearShowStr {
NSString *yearStr = @"";
JXDateFirstModel *fModel = self.dataSourceArr[self.deFirstRow];
yearStr = [NSString stringWithFormat:@"%@",fModel.title];
return yearStr;
}
/// 周选择数据源
- (void)weekDataSource {
NSDateComponents *minDateComponet = [self.miniDate YMDComponents];
NSInteger minYear = minDateComponet.year;
NSDateComponents *maxDateComponet = [self.maxDate YMDComponents];
NSInteger maxYear = maxDateComponet.year;
//默认日期的
NSDateComponents *deaultComponet = [self.defalutDate YMDComponents];
NSInteger deaulYear = deaultComponet.year;
NSInteger deaulWeekNumber = [NSDate getWeekNumberOfYearWithDate:self.defalutDate];
for (NSInteger i = minYear; i <= maxYear; i ++) {
JXDateFirstModel *fModel = [[JXDateFirstModel alloc] init];
fModel.name = [NSString stringWithFormat:@"%ld",(long)i];
fModel.title = [NSString stringWithFormat:@"%ld年",(long)i];
NSMutableArray *monthArr = [NSMutableArray array];
NSInteger stratWeek = i == minYear ? [NSDate getWeekNumberOfYearWithDate:self.miniDate] : 1;
NSInteger endWeek = i == maxYear ? [NSDate getWeekNumberOfYearWithDate:self.maxDate] : [NSDate getWeekNumberOfYear:i];
NSLog(@"周周%ld****%ld", (long)stratWeek, (long)endWeek);
for (NSInteger j = stratWeek; j <= endWeek; j ++) {
JXDateSecondModel *sMothModel = [[JXDateSecondModel alloc] init];
sMothModel.weekNumber = j;
NSDate *start = [NSDate getWeekRangeStartDate_Year:i WeakOfYear:j];
NSDate *end = [NSDate getWeekRangeEndDate_Year:i WeakOfYear:j];
sMothModel.name = [WTCommonUtils formatMonthZero:j];
sMothModel.title = [self weekShowStringWithNum:j startDate:start endDate:end];
if (i == deaulYear && j == deaulWeekNumber) {
self.deFirstRow = i - minYear;
self.deSecondRow = j - stratWeek;
}
[monthArr addObject:sMothModel];
}
fModel.secondArray = monthArr;
[self.dataSourceArr addObject:fModel];
}
}
/// 拼接周展示
/// @param week 第几周
/// @param sDate 开始
/// @param eDate 结束
- (NSString *)weekShowStringWithNum:(NSInteger)week startDate:(NSDate *)sDate endDate:(NSDate *)eDate {
NSString *str = [NSString stringWithFormat:@"第%ld周 ",(long)week];
NSString *start = [sDate dateFormatterString:@"MM/dd"];
NSString *end = [eDate dateFormatterString:@"MM/dd"];
str = [NSString stringWithFormat:@"%@%@-%@",str,start,end];
return str;
}
/// 日选择数据源
- (void)dayTypeModel {
NSDateComponents *minDateComponet = [self.miniDate YMDComponents];
NSInteger minYear = minDateComponet.year;
NSInteger miniMonth = minDateComponet.month;
NSInteger miniDay = minDateComponet.day;
NSDateComponents *maxDateComponet = [self.maxDate YMDComponents];
NSInteger maxYear = maxDateComponet.year;
NSInteger maxMonth = maxDateComponet.month;
NSInteger maxDay = maxDateComponet.day;
NSDateComponents *deaultComponet = [self.defalutDate YMDComponents];
NSInteger deaulYear = deaultComponet.year;
NSInteger deaulMonth = deaultComponet.month;
NSInteger deaulDay = deaultComponet.day;
for (NSInteger i = minYear; i <= maxYear; i ++) {
JXDateFirstModel *fModel = [[JXDateFirstModel alloc] init];
fModel.name = [NSString stringWithFormat:@"%ld",(long)i];
fModel.title = [NSString stringWithFormat:@"%ld年",(long)i];
NSMutableArray *monthArr = [NSMutableArray array];
NSInteger stratMonth = i == minYear ? miniMonth : 1;
NSInteger endMonth = i == maxYear ? maxMonth : 12;
for (NSInteger j = stratMonth; j <= endMonth; j ++) {
JXDateSecondModel *sMothModel = [[JXDateSecondModel alloc] init];
sMothModel.name = [WTCommonUtils formatMonthZero:j];
sMothModel.title = [NSString stringWithFormat:@"%@月",[WTCommonUtils formatMonthZero:j]];
NSDate *motnDate = [NSDate dateWithString:[NSString stringWithFormat:@"%ld-%@",(long)i,[WTCommonUtils formatMonthZero:j]] formatterString:@"yyyy-MM"];
NSInteger monthDay = [motnDate numberOfDaysInCurrentMonth];
NSMutableArray *dayArr= [NSMutableArray array];
NSInteger startDay = i == minYear && j == miniMonth ? miniDay : 1;
NSInteger endDay = i == maxYear && j == maxMonth ? maxDay : monthDay;
for (NSInteger k = startDay; k <= endDay; k ++) {
JXDateThirdModel *dayModels = [[JXDateThirdModel alloc] init];
dayModels.name = [WTCommonUtils formatMonthZero:k];
dayModels.title = [NSString stringWithFormat:@"%@日",dayModels.name];
if (i == deaulYear && j == deaulMonth && k == deaulDay) {
self.deFirstRow = i - minYear;
self.deSecondRow = j - stratMonth;
NSLog(@"%ld ** %ld", (long)k ,(long)startDay);
self.deThirdRow = k - startDay;
}
[dayArr addObject:dayModels];
}
sMothModel.thirdArray = dayArr;
[monthArr addObject:sMothModel];
}
fModel.secondArray = monthArr;
[self.dataSourceArr addObject:fModel];
}
}
/// 月选择数据源
- (void)monthTypeSource {
NSDateComponents *minDateComponet = [self.miniDate YMDComponents];
NSInteger minYear = minDateComponet.year;
NSInteger miniMonth = minDateComponet.month;
NSDateComponents *maxDateComponet = [self.maxDate YMDComponents];
NSInteger maxYear = maxDateComponet.year;
NSInteger maxMonth = maxDateComponet.month;
NSDateComponents *deaultComponet = [self.defalutDate YMDComponents];
NSInteger deaulYear = deaultComponet.year;
NSInteger deaulMonth = deaultComponet.month;
for (NSInteger i = minYear; i <= maxYear; i ++) {
JXDateFirstModel *fModel = [[JXDateFirstModel alloc] init];
fModel.name = [NSString stringWithFormat:@"%ld",(long)i];
fModel.title = [NSString stringWithFormat:@"%ld年",(long)i];
NSMutableArray *monthArr = [NSMutableArray array];
NSInteger stratMonth = i == minYear ? miniMonth : 1;
NSInteger endMonth = i == maxYear ? maxMonth : 12;
for (NSInteger j = stratMonth; j <= endMonth; j ++) {
JXDateSecondModel *sMothModel = [[JXDateSecondModel alloc] init];
sMothModel.name = [WTCommonUtils formatMonthZero:j];
sMothModel.title = [NSString stringWithFormat:@"%@月",[WTCommonUtils formatMonthZero:j]];
if (i == deaulYear && j == deaulMonth) {
self.deFirstRow = i - minYear;
self.deSecondRow = j - stratMonth;
}
[monthArr addObject:sMothModel];
}
fModel.secondArray = monthArr;
[self.dataSourceArr addObject:fModel];
}
}
/// 年选择数据源
- (void)yearTypeSource {
NSDateComponents *minDateComponet = [self.miniDate YMDComponents];
NSInteger minYear = minDateComponet.year;
NSDateComponents *maxDateComponet = [self.maxDate YMDComponents];
NSInteger maxYear = maxDateComponet.year;
NSDateComponents *deaultComponet = [self.defalutDate YMDComponents];
NSInteger deaulYear = deaultComponet.year;
for (NSInteger i = minYear; i <= maxYear; i ++) {
JXDateFirstModel *fModel = [[JXDateFirstModel alloc] init];
fModel.name = [NSString stringWithFormat:@"%ld",(long)i];
fModel.title = [NSString stringWithFormat:@"%ld年",(long)i];
if (i == deaulYear) {
self.deFirstRow = i - minYear;
}
[self.dataSourceArr addObject:fModel];
}
}
/// 季选择数据源
- (void)seasonTypeSource {
NSDateComponents *minDateComponet = [self.miniDate YMDComponents];
NSInteger minYear = minDateComponet.year;
NSInteger miniMonth = minDateComponet.month;
NSDateComponents *maxDateComponet = [self.maxDate YMDComponents];
NSInteger maxYear = maxDateComponet.year;
NSInteger maxMonth = maxDateComponet.month;
NSDateComponents *deaultComponet = [self.defalutDate YMDComponents];
NSInteger deaulYear = deaultComponet.year;
NSInteger deaulMonth = deaultComponet.month;
for (NSInteger i = minYear; i <= maxYear; i ++) {
JXDateFirstModel *fModel = [[JXDateFirstModel alloc] init];
fModel.name = [NSString stringWithFormat:@"%ld",(long)i];
fModel.title = [NSString stringWithFormat:@"%ld年",(long)i];
NSMutableArray *monthArr = [NSMutableArray array];
NSInteger stratMonth = i == minYear ? miniMonth : 1;
NSInteger endMonth = i == maxYear ? maxMonth : 12;
NSInteger startSeason = ceilf(stratMonth / 3.0);
NSInteger endSeason = ceilf(endMonth / 3.0);
for (NSInteger j = startSeason; j <= endSeason; j ++) {
JXDateSecondModel *sMothModel = [[JXDateSecondModel alloc] init];
sMothModel.name = [NSString stringWithFormat:@"%ld",(long)j];
sMothModel.title = [NSString stringWithFormat:@"%@",[WTCommonUtils seasonChineses:j]];
if (i == deaulYear && j == ceilf(deaulMonth / 3.0)) {
self.deFirstRow = i - minYear;
self.deSecondRow = j - startSeason;
}
[monthArr addObject:sMothModel];
}
fModel.secondArray = monthArr;
[self.dataSourceArr addObject:fModel];
}
}
/// 旬选择数据源
- (void)meadowTypeModel {
NSDateComponents *minDateComponet = [self.miniDate YMDComponents];
NSInteger minYear = minDateComponet.year;
NSInteger miniMonth = minDateComponet.month;
NSInteger miniDay = minDateComponet.day;
NSDateComponents *maxDateComponet = [self.maxDate YMDComponents];
NSInteger maxYear = maxDateComponet.year;
NSInteger maxMonth = maxDateComponet.month;
NSInteger maxDay = maxDateComponet.day;
NSDateComponents *deaultComponet = [self.defalutDate YMDComponents];
NSInteger deaulYear = deaultComponet.year;
NSInteger deaulMonth = deaultComponet.month;
NSInteger deaulDay = deaultComponet.day;
NSInteger deaulMeadow = ceilf(deaulDay / 10.0) > 3 ? 3 : ceilf(deaulDay / 10.0);
for (NSInteger i = minYear; i <= maxYear; i ++) {
JXDateFirstModel *fModel = [[JXDateFirstModel alloc] init];
fModel.name = [NSString stringWithFormat:@"%ld",(long)i];
fModel.title = [NSString stringWithFormat:@"%ld年",(long)i];
NSMutableArray *monthArr = [NSMutableArray array];
NSInteger stratMonth = i == minYear ? miniMonth : 1;
NSInteger endMonth = i == maxYear ? maxMonth : 12;
for (NSInteger j = stratMonth; j <= endMonth; j ++) {
JXDateSecondModel *sMothModel = [[JXDateSecondModel alloc] init];
sMothModel.name = [WTCommonUtils formatMonthZero:j];
sMothModel.title = [NSString stringWithFormat:@"%@月",[WTCommonUtils formatMonthZero:j]];
NSDate *motnDate = [NSDate dateWithString:[NSString stringWithFormat:@"%ld-%@",(long)i,[WTCommonUtils formatMonthZero:j]] formatterString:@"yyyy-MM"];
NSInteger monthDay = [motnDate numberOfDaysInCurrentMonth];
NSMutableArray *dayArr= [NSMutableArray array];
NSInteger startDay = i == minYear && j == miniMonth ? miniDay : 1;
NSInteger endDay = i == maxYear && j == maxMonth ? maxDay : monthDay;
NSInteger stratMeadow = ceilf(startDay / 10.0) > 3? 3:ceilf(startDay / 10.0);
NSInteger endMeadow = ceilf(endDay / 10.0) > 3 ? 3 : ceilf(endDay / 10.0);
for (NSInteger k = stratMeadow; k <= endMeadow; k ++) {
JXDateThirdModel *dayModels = [[JXDateThirdModel alloc] init];
dayModels.name = [WTCommonUtils meadowChinesesJudegWithDay:k];
dayModels.meadow = k;
dayModels.title = [WTCommonUtils meadowChinesesJudegWithDay:k];
if (i == deaulYear && j == deaulMonth && k == deaulMeadow) {
self.deFirstRow = i - minYear;
self.deSecondRow = j - stratMonth;
self.deThirdRow = k - stratMeadow;
}
[dayArr addObject:dayModels];
}
sMothModel.thirdArray = dayArr;
[monthArr addObject:sMothModel];
}
fModel.secondArray = monthArr;
[self.dataSourceArr addObject:fModel];
}
}
/// 向后退一个日期
- (void)backwardDate {
if (self.deFirstRow == 0 && self.deSecondRow == 0 && self.deThirdRow == 0) {
[WTProgressHUD showToastWithStatus:@"不能小于该日期"];
return;
}
if (self.dateType == DatePickerTypeDay) {//日
[self dayBackFoward];
} else if (self.dateType == DatePickerTypeWeek) {//周
[self weekBackForward];
} else if (self.dateType == DatePickerTypeMeadow) { //旬
[self dayBackFoward];
} else if (self.dateType == DatePickerTypeMonth) {//月
[self weekBackForward];
} else if (self.dateType == DatePickerTypeSeason) {//季度
[self weekBackForward];
} else { //年
if (self.deFirstRow > 0) {
self.deFirstRow = self.deFirstRow - 1;
}
}
}
/// 天 三列的 后退一个日期
- (void)dayBackFoward {
JXDateFirstModel *fModel = self.dataSourceArr[self.deFirstRow];
if (self.deThirdRow > 0) { //大于0的索引
self.deThirdRow = self.deThirdRow - 1;
} else { //等于0
if (self.deSecondRow > 0) {
self.deSecondRow = self.deSecondRow - 1;
JXDateSecondModel *sModel = fModel.secondArray[self.deSecondRow];
self.deThirdRow = sModel.thirdArray.count - 1;
} else { //月份索引为 0
if (self.deFirstRow > 0) {
self.deFirstRow = self.deFirstRow - 1;
fModel = self.dataSourceArr[self.deFirstRow];
self.deSecondRow = fModel.secondArray.count - 1;
JXDateSecondModel *sModel = fModel.secondArray[self.deSecondRow];
self.deThirdRow = sModel.thirdArray.count - 1;
} else {
[WTProgressHUD showToastWithStatus:@"不能小于该日期"];
}
}
}
}
/// 周 往后退一个周期 两列的
- (void)weekBackForward {
JXDateFirstModel *fModel;
if (self.deSecondRow > 0) { //大于0
self.deSecondRow = self.deSecondRow - 1;
} else { //等于0
if (self.deFirstRow > 0) {
self.deFirstRow = self.deFirstRow - 1;
fModel = self.dataSourceArr[self.deFirstRow];
self.deSecondRow = fModel.secondArray.count - 1;
}
}
}
/// 向右一个日期 向前
- (void)forwardDate {
if (self.dateType == DatePickerTypeDay) {//日
[self dayDateForward];
} else if (self.dateType == DatePickerTypeWeek) {//周
[self weekDateForward];
} else if (self.dateType == DatePickerTypeMeadow) { //旬
[self dayDateForward];
} else if (self.dateType == DatePickerTypeMonth) {//月
[self weekDateForward];
} else if (self.dateType == DatePickerTypeSeason) {//季度
[self weekDateForward];
} else { //年
if (self.deFirstRow < self.dataSourceArr.count - 1) {
self.deFirstRow = self.deFirstRow + 1;
}
}
}
/// 向后加一天
- (void)dayDateForward {
JXDateFirstModel *fModel = self.dataSourceArr[self.deFirstRow];
JXDateSecondModel *sModel = fModel.secondArray[self.deSecondRow];
if (self.deThirdRow < sModel.thirdArray.count - 1) {
self.deThirdRow = self.deThirdRow + 1;
} else {
if (self.deSecondRow < fModel.secondArray.count - 1) {
self.deSecondRow = self.deSecondRow + 1;
self.deThirdRow = 0;
} else { //看第一行
if (self.deFirstRow < self.dataSourceArr.count - 1) {
self.deFirstRow = self.deFirstRow + 1;
self.deSecondRow = 0;
self.deThirdRow = 0;
}
}
}
}
/// 周向右 加一个日期 两行的
- (void)weekDateForward {
JXDateFirstModel *fModel = self.dataSourceArr[self.deFirstRow];
if (self.deSecondRow < fModel.secondArray.count - 1) {
self.deSecondRow = self.deSecondRow + 1;
} else {
if (self.deFirstRow < self.dataSourceArr.count - 1) {
self.deFirstRow = self.deFirstRow + 1;
self.deSecondRow = 0;
}
}
}
- (NSMutableArray *)dataSourceArr {
if (!_dataSourceArr) {
_dataSourceArr = [NSMutableArray array];
}
return _dataSourceArr;
}
@end
自定义日期选择器 JXCustomDatePicker
/** 自定义日期选择器 */
@interface JXCustomDatePicker : UIView
/// 日期类型
@property (nonatomic,assign)DatePickerType type;
/** 最小日期 */
@property (strong, nonatomic) NSDate *miniDate;
/** 最大日期 */
@property (strong, nonatomic) NSDate *maxDate;
/** 默认日期 */
@property (strong, nonatomic) NSDate *defalutDate;
/// 回调刷新选择
@property (nonatomic,copy)void (^sureBlock)(NSString *str);
@end
@interface JXCustomDatePicker ()
{
NSInteger defaulFirstRow;
NSInteger defaulSecondRow;
NSInteger defaulThirdRow;
}
/** 阴影 */
@property (weak, nonatomic) IBOutlet UIView *shadowView;
/** 选择器背景 */
@property (weak, nonatomic) IBOutlet UIView *pickerBackView;
/** 日选择器 */
@property (strong, nonatomic) UIDatePicker *dayPickerView;
/** 选择器 */
@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
/** 数据源数组*/
@property (strong, nonatomic) NSMutableArray *dataSourceArr;
/** 数据源模型 */
@property (strong, nonatomic) JXDateFirstModel *selectFirst;
@property (strong, nonatomic) JXDateSecondModel *selcetSecond;
@property (strong, nonatomic) JXDateThirdModel *selectThird;
@end
@implementation JXCustomDatePicker
- (void)awakeFromNib {
[super awakeFromNib];
[self.shadowView wt_addShadowNormalWithShadowRadius:14.0 offset:CGSizeMake(0,4) opacity:1.0 color:[UIColor colorWithRed:51/255.0 green:119/255.0 blue:255/255.0 alpha:0.5] cornerRadius:0.0];
self.pickerView.delegate = self;
self.pickerView.dataSource = self;
self.defalutDate = [NSDate date];
self.maxDate = [NSDate date];
self.miniDate = [NSDate dateWithString:@"2020-01-01" formatterString:@"yyyy-MM-dd"];
}
- (void)setType:(DatePickerType)type {
_type = type;
[self.dataSourceArr removeAllObjects];
if (type == DatePickerTypeDay) {
[self dayTypeModel];
} else if (type == DatePickerTypeMonth) {
[self monthTypeSource];
} else if (type == DatePickerTypeYear) {
[self yearTypeSource];
} else if (type == DatePickerTypeSeason) {
[self seasonTypeSource];
} else if (type == DatePickerTypeMeadow) {
[self meadowTypeModel];
} else if (type == DatePickerTypeWeek) {
[self weekDataSource];
}
[self.pickerView reloadAllComponents];
[self selectDealShow];
}
/// 展示默认数据选择项
- (void)selectDealShow {
switch (self.type) {
case DatePickerTypeDay:
case DatePickerTypeMeadow:
{
[self.pickerView selectRow:defaulFirstRow inComponent:0 animated:YES];
[self.pickerView selectRow:defaulSecondRow inComponent:1 animated:YES];
[self.pickerView selectRow:defaulThirdRow inComponent:2 animated:YES];
}
break;
case DatePickerTypeWeek:
case DatePickerTypeMonth:
case DatePickerTypeSeason:
{
[self.pickerView selectRow:defaulFirstRow inComponent:0 animated:YES];
[self.pickerView selectRow:defaulSecondRow inComponent:1 animated:YES];
}
break;
case DatePickerTypeYear:
{
[self.pickerView selectRow:defaulFirstRow inComponent:0 animated:YES];
}
break;
default: {
}
break;
}
}
/// 日选择数据源
- (void)dayTypeModel {
NSDateComponents *minDateComponet = [self.miniDate YMDComponents];
NSInteger minYear = minDateComponet.year;
NSInteger miniMonth = minDateComponet.month;
NSInteger miniDay = minDateComponet.day;
NSDateComponents *maxDateComponet = [self.maxDate YMDComponents];
NSInteger maxYear = maxDateComponet.year;
NSInteger maxMonth = maxDateComponet.month;
NSInteger maxDay = maxDateComponet.day;
NSDateComponents *deaultComponet = [self.defalutDate YMDComponents];
NSInteger deaulYear = deaultComponet.year;
NSInteger deaulMonth = deaultComponet.month;
NSInteger deaulDay = deaultComponet.day;
for (NSInteger i = minYear; i <= maxYear; i ++) {
JXDateFirstModel *fModel = [[JXDateFirstModel alloc] init];
fModel.name = [NSString stringWithFormat:@"%ld",(long)i];
fModel.title = [NSString stringWithFormat:@"%ld年",(long)i];
NSMutableArray *monthArr = [NSMutableArray array];
NSInteger stratMonth = i == minYear ? miniMonth : 1;
NSInteger endMonth = i == maxYear ? maxMonth : 12;
for (NSInteger j = stratMonth; j <= endMonth; j ++) {
JXDateSecondModel *sMothModel = [[JXDateSecondModel alloc] init];
sMothModel.name = [WTCommonUtils formatMonthZero:j];
sMothModel.title = [NSString stringWithFormat:@"%@月",[WTCommonUtils formatMonthZero:j]];
NSDate *motnDate = [NSDate dateWithString:[NSString stringWithFormat:@"%ld-%@",(long)i,[WTCommonUtils formatMonthZero:j]] formatterString:@"yyyy-MM"];
NSInteger monthDay = [motnDate numberOfDaysInCurrentMonth];
NSMutableArray *dayArr= [NSMutableArray array];
NSInteger startDay = i == minYear && j == miniMonth ? miniDay : 1;
NSInteger endDay = i == maxYear && j == maxMonth ? maxDay : monthDay;
for (NSInteger k = startDay; k <= endDay; k ++) {
JXDateThirdModel *dayModels = [[JXDateThirdModel alloc] init];
dayModels.name = [WTCommonUtils formatMonthZero:k];
dayModels.title = [NSString stringWithFormat:@"%@日",dayModels.name];
if (i == deaulYear && j == deaulMonth && k == deaulDay) {
defaulFirstRow = i - minYear;
defaulSecondRow = j - stratMonth;
NSLog(@"%ld ** %ld", (long)k ,(long)startDay);
defaulThirdRow = k - startDay;
}
[dayArr addObject:dayModels];
}
sMothModel.thirdArray = dayArr;
[monthArr addObject:sMothModel];
}
fModel.secondArray = monthArr;
[self.dataSourceArr addObject:fModel];
}
}
/// 确定点击
/// @param sender 按钮
- (IBAction)sureButtonClick:(UIButton *)sender {
[self removeFromSuperview];
switch (self.type) {
case DatePickerTypeDay: {
[self dayVauleSure];
}
break;
case DatePickerTypeMonth: {
[self monthValueSure];
}
break;
case DatePickerTypeYear: {
[self yaerValueSure];
}
break;
case DatePickerTypeSeason: {
[self seasonValueSure];
}
break;
case DatePickerTypeMeadow: {
[self meadowValueSure];
}
break;
case DatePickerTypeWeek: {
[self weekValueSelectSure];
}
break;
default:
break;
}
}
/// 日选择确认
- (void)dayVauleSure {
NSInteger firstIndex = [self.pickerView selectedRowInComponent:0];
NSInteger secondIndex = [self.pickerView selectedRowInComponent:1];
NSInteger lastIndex = [self.pickerView selectedRowInComponent:2];
[DateService shareManager].deFirstRow = firstIndex;
[DateService shareManager].deSecondRow = secondIndex;
[DateService shareManager].deThirdRow = lastIndex;
JXDateFirstModel *fModel = self.dataSourceArr[firstIndex];
JXDateSecondModel *sModel = fModel.secondArray[secondIndex];
JXDateThirdModel *tModel = sModel.thirdArray[lastIndex];
NSString *dayStr = [NSString stringWithFormat:@"%@/%@/%@",fModel.name,sModel.name,tModel.name];
if (self.sureBlock) {
self.sureBlock(dayStr);
}
}
/// 月份确定
- (void)monthValueSure {
NSInteger firstIndex = [self.pickerView selectedRowInComponent:0];
NSInteger secondIndex = [self.pickerView selectedRowInComponent:1];
[DateService shareManager].deFirstRow = firstIndex;
[DateService shareManager].deSecondRow = secondIndex;
[DateService shareManager].deThirdRow = 0;
JXDateFirstModel *fModel = self.dataSourceArr[firstIndex];
JXDateSecondModel *sModel = fModel.secondArray[secondIndex];
NSString *dayStr = [NSString stringWithFormat:@"%@/%@",fModel.name,sModel.name];
if (self.sureBlock) {
self.sureBlock(dayStr);
}
}
/// 年确定
- (void)yaerValueSure {
NSInteger firstIndex = [self.pickerView selectedRowInComponent:0];
[DateService shareManager].deFirstRow = firstIndex;
[DateService shareManager].deSecondRow = 0;
[DateService shareManager].deThirdRow = 0;
JXDateFirstModel *fModel = self.dataSourceArr[firstIndex];
NSString *dayStr = [NSString stringWithFormat:@"%@",fModel.title];
if (self.sureBlock) {
self.sureBlock(dayStr);
}
}
/// 季度选择确定
- (void)seasonValueSure {
NSInteger firstIndex = [self.pickerView selectedRowInComponent:0];
NSInteger secondIndex = [self.pickerView selectedRowInComponent:1];
[DateService shareManager].deFirstRow = firstIndex;
[DateService shareManager].deSecondRow = secondIndex;
[DateService shareManager].deThirdRow = 0;
JXDateFirstModel *fModel = self.dataSourceArr[firstIndex];
JXDateSecondModel *sModel = fModel.secondArray[secondIndex];
NSString *dayStr = [NSString stringWithFormat:@"%@ %@",fModel.name,sModel.name];
if (self.sureBlock) {
self.sureBlock(dayStr);
}
}
/// 旬确定
- (void)meadowValueSure {
NSInteger firstIndex = [self.pickerView selectedRowInComponent:0];
NSInteger secondIndex = [self.pickerView selectedRowInComponent:1];
NSInteger lastIndex = [self.pickerView selectedRowInComponent:2];
[DateService shareManager].deFirstRow = firstIndex;
[DateService shareManager].deSecondRow = secondIndex;
[DateService shareManager].deThirdRow = lastIndex;
JXDateFirstModel *fModel = self.dataSourceArr[firstIndex];
JXDateSecondModel *sModel = fModel.secondArray[secondIndex];
JXDateThirdModel *tModel = sModel.thirdArray[lastIndex];
NSString *dayStr = [NSString stringWithFormat:@"%@/%@/%@",fModel.name,sModel.name,tModel.name];
if (self.sureBlock) {
self.sureBlock(dayStr);
}
}
/// 周数据选择确定
- (void)weekValueSelectSure {
NSInteger firstIndex = [self.pickerView selectedRowInComponent:0];
NSInteger secondIndex = [self.pickerView selectedRowInComponent:1];
[DateService shareManager].deFirstRow = firstIndex;
[DateService shareManager].deSecondRow = secondIndex;
[DateService shareManager].deThirdRow = 0;
JXDateFirstModel *fModel = self.dataSourceArr[firstIndex];
JXDateSecondModel *sModel = fModel.secondArray[secondIndex];
NSInteger yearWeek = [fModel.name integerValue];
NSDate *star = [NSDate getWeekRangeStartDate_Year:yearWeek WeakOfYear:sModel.weekNumber];
NSDate *endDay = [NSDate getWeekRangeEndDate_Year:yearWeek WeakOfYear:sModel.weekNumber];
NSString *dayStr = [NSString stringWithFormat:@"%@-%@",[star dateFormatterString:@"yyyy/MM/dd"],[endDay dateFormatterString:@"yyyy/MM/dd"]];
if (self.sureBlock) {
self.sureBlock(dayStr);
}
}
#pragma mark - dataSouce
// Components
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
if (self.dataSourceArr.count == 0) {
return 0;
}
NSInteger compNumber = 0;
if (self.type == DatePickerTypeDay) {
compNumber = 3;
} else if (self.type == DatePickerTypeWeek) {
compNumber = 2;
} else if (self.type == DatePickerTypeMeadow) {
compNumber = 3;
} else if (self.type == DatePickerTypeMonth) {
compNumber = 2;
} else if (self.type == DatePickerTypeSeason) {
compNumber = 2;
} else if (self.type == DatePickerTypeYear) {
compNumber = 1;
}
return compNumber;
}
//Rows
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (0 == component) {
return self.dataSourceArr.count;
} else if (component == 1) {
JXDateFirstModel *firstModel = self.dataSourceArr[defaulFirstRow];
NSLog(@"***%lu", (unsigned long)firstModel.secondArray.count);
_selectFirst = firstModel;
return firstModel.secondArray.count;
} else {
JXDateSecondModel *secondM = _selectFirst.secondArray[defaulSecondRow];
_selcetSecond = secondM;
NSLog(@"*****%lu", (unsigned long)secondM.thirdArray.count);
return secondM.thirdArray.count;
}
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
switch (self.type) {
case DatePickerTypeDay:
case DatePickerTypeMeadow:
{
return ScreenWidth / 3.0;
}
break;
case DatePickerTypeWeek:
{
switch (component) {
case 0:
return ScreenWidth * 0.3;
break;
default:
return ScreenWidth * 0.7;
break;
}
}
break;
case DatePickerTypeMonth:
case DatePickerTypeSeason:
{
switch (component) {
case 0:
return ScreenWidth * 0.5;
break;
default:
return ScreenWidth * 0.5;
break;
}
}
break;
default: {
return ScreenWidth;
}
break;
}
}
//title
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if (component == 0) {
JXDateFirstModel *firstM = self.dataSourceArr[row];
return firstM.title;
} else if (component == 1) {
JXDateSecondModel *secondM = _selectFirst.secondArray[row];
return secondM.title;
} else {
JXDateThirdModel *thirdModel = _selcetSecond.thirdArray[row];
return thirdModel.title;
}
}
#pragma mark - delegate
//Select
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
NSLog(@"选中拉拉%ld**%ld", (long)component,row);
[self afterSelectDelegateDealShowWithComponet:component];
}
- (void)afterSelectDelegateDealShowWithComponet:(NSInteger)component {
switch (self.type) {
case DatePickerTypeDay:
case DatePickerTypeMeadow:
{
if (component == 0) {
[self.pickerView reloadAllComponents];
[self.pickerView selectRow:0 inComponent:1 animated:YES];
[self.pickerView selectRow:0 inComponent:2 animated:YES];
} else if (component == 1) {
[self.pickerView reloadAllComponents];
[self.pickerView selectRow:0 inComponent:2 animated:YES];
} else {
}
NSInteger firstIndex = [self.pickerView selectedRowInComponent:0];
NSInteger secondIndex = [self.pickerView selectedRowInComponent:1];
NSInteger lastIndex = [self.pickerView selectedRowInComponent:2];
defaulFirstRow = firstIndex;
defaulSecondRow = secondIndex;
defaulThirdRow = lastIndex;
[self.pickerView reloadAllComponents];
JXDateFirstModel *fModel = self.dataSourceArr[firstIndex];
JXDateSecondModel *sModel = fModel.secondArray[secondIndex];
JXDateThirdModel *tModel = sModel.thirdArray[lastIndex];
NSLog(@"%@**%@**%@",fModel.title,sModel.title,tModel.title);
}
break;
case DatePickerTypeWeek:
case DatePickerTypeMonth:
case DatePickerTypeSeason:
{
if (component == 0) {
[self.pickerView reloadAllComponents];
[self.pickerView selectRow:0 inComponent:1 animated:YES];
} else {
}
NSInteger firstIndex = [self.pickerView selectedRowInComponent:0];
NSInteger secondIndex = [self.pickerView selectedRowInComponent:1];
defaulFirstRow = firstIndex;
defaulSecondRow = secondIndex;
[self.pickerView reloadAllComponents];
JXDateFirstModel *fModel = self.dataSourceArr[firstIndex];
JXDateSecondModel *sModel = fModel.secondArray[secondIndex];
NSLog(@"%@**%@",fModel.title,sModel.title);
}
break;
case DatePickerTypeYear:
{
NSInteger selectFirstIndex = [self.pickerView selectedRowInComponent:0];
defaulFirstRow = selectFirstIndex;
NSInteger firstIndex = [self.pickerView selectedRowInComponent:0];
JXDateFirstModel *fModel = self.dataSourceArr[firstIndex];
NSLog(@"%@",fModel.title);
}
break;
default: {
}
break;
}
}
- (NSMutableArray *)dataSourceArr {
if (!_dataSourceArr) {
_dataSourceArr = [NSMutableArray array];
}
return _dataSourceArr;
}
使用
BIWeakObj(self) //日期选择框弹起
[self.dateView addTapGestureBlock:^{
if ([selfWeak.datePickerView superview]) {
[selfWeak.datePickerView removeFromSuperview];
} else {
[selfWeak.view addSubview:selfWeak.datePickerView];
selfWeak.datePickerView.type = selfWeak.dateType;
}
}];
[DateService shareManager].dateType = self.dateType;
- (JXCustomDatePicker *)datePickerView {
if (!_datePickerView) {
_datePickerView = [JXCustomDatePicker view];
_datePickerView.frame = CGRectMake(0, 42.0, ScreenWidth, ScreenHeight - kNavbarHeight);
BIWeakObj(self)
_datePickerView.sureBlock = ^(NSString * _Nonnull str) {
selfWeak.dateLable.text = str;
selfWeak.startDate = [[DateService shareManager] startDateWithDate];
selfWeak.endDate = [[DateService shareManager] endDateWithDate];
[selfWeak dataRequest];
};
}
return _datePickerView;
}