// // DateManager.m // YouHuiProject // // Created by 小花 on 2018/7/9. // Copyright © 2018年 kuxuan. All rights reserved. // #import "DateManager.h" #import "DateModel.h" @implementation DateManager + (NSArray *)getBuyLimitDateModels { NSMutableArray *array = [self getBuyLimitTimestrArr]; NSDateFormatter *formate = [[NSDateFormatter alloc] init]; [formate setTimeZone:[NSTimeZone localTimeZone]]; [formate setDateFormat:@"yyyy-MM-dd HH:mm"]; for (int i = 0; i < array.count; i++) { DateModel *model = array[i]; NSDate *startDate = [formate dateFromString:model.startDateStr]; NSTimeInterval startDateInterval = [startDate timeIntervalSince1970]; NSDate *endDate = [formate dateFromString:model.endDateStr]; NSTimeInterval endDateInterval = [endDate timeIntervalSince1970]; NSDate *date = [NSDate date]; NSTimeInterval localeDateInterval = [date timeIntervalSince1970]; if (localeDateInterval >= startDateInterval && localeDateInterval <= endDateInterval) { return [self subModelFromModelArr:array index:i]; } } return [self subModelFromModelArr:array index:0]; } + (NSArray *)subModelFromModelArr:(NSMutableArray *)totalArr index:(NSInteger)index{ NSInteger local = index-7>0 ? index-7:0; NSArray *dateArr = [totalArr subarrayWithRange:NSMakeRange(local, 15)]; return dateArr; } + (NSMutableArray *)getBuyLimitTimestrArr { NSString *curDateStr = [self getCurrentTime]; NSString *yesDateStr = [self GetYesterdayDay:[NSDate date]]; NSString *tomDateStr = [self GetTomorrowDay:[NSDate date]]; NSArray *timeArr = @[@"00:00",@"08:00",@"10:00",@"11:00",@"12:00",@"13:00",@"14:00",@"15:00",@"17:00",@"19:00",@"21:00",@"22:00",@"23:00"]; NSMutableArray *yesTimeArr = [NSMutableArray array]; NSMutableArray *todTimeArr = [NSMutableArray array]; NSMutableArray *tomTimeArr = [NSMutableArray array]; for (NSString *timeStr in timeArr) { NSString *yesTimeStr = [NSString stringWithFormat:@"%@ %@",yesDateStr,timeStr]; NSString *todTimeStr = [NSString stringWithFormat:@"%@ %@",curDateStr,timeStr]; NSString *tomTimeStr = [NSString stringWithFormat:@"%@ %@",tomDateStr,timeStr]; DateModel *yesModel = [[DateModel alloc] init]; yesModel.endTimeStr = timeStr; yesModel.endDateStr = yesTimeStr; DateModel *todModel = [[DateModel alloc] init]; todModel.endTimeStr = timeStr; todModel.endDateStr = todTimeStr; DateModel *tomModel = [[DateModel alloc] init]; tomModel.endTimeStr = timeStr; tomModel.endDateStr = tomTimeStr; [yesTimeArr addObject:yesModel]; [todTimeArr addObject:todModel]; [tomTimeArr addObject:tomModel]; } NSMutableArray *totalArr = [NSMutableArray array]; [totalArr addObjectsFromArray:yesTimeArr]; [totalArr addObjectsFromArray:todTimeArr]; [totalArr addObjectsFromArray:tomTimeArr]; for (int i = 0; i < totalArr.count; i++) { DateModel *model = totalArr[i]; if (i==0) { model.startTimeStr = @"23:00"; model.startDateStr = [NSString stringWithFormat:@"%@ %@",curDateStr,@"23:00"]; }else if (i==totalArr.count-1){ model.startTimeStr = @"00:00"; model.startDateStr = [NSString stringWithFormat:@"%@ %@",curDateStr,@"00:00"]; }else { DateModel *lastModel = totalArr[i-1]; model.startTimeStr = lastModel.endTimeStr; model.startDateStr = lastModel.endDateStr; } } return totalArr; } //获取当地时间 + (NSString *)getCurrentTime { NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"yyyy-MM-dd"]; NSString *dateTime = [formatter stringFromDate:[NSDate date]]; return dateTime; } //将字符串转成NSDate类型 + (NSDate *)dateFromString:(NSString *)dateString { NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat: @"yyyy-MM-dd"]; NSDate *destDate= [dateFormatter dateFromString:dateString]; return destDate; } //传入今天的时间,返回明天的时间 + (NSString *)GetTomorrowDay:(NSDate *)aDate { NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; NSDateComponents *components = [gregorian components:NSCalendarUnitWeekday | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:aDate]; [components setDay:([components day]+1)]; NSDate *beginningOfWeek = [gregorian dateFromComponents:components]; NSDateFormatter *dateday = [[NSDateFormatter alloc] init]; [dateday setDateFormat:@"yyyy-MM-dd"]; return [dateday stringFromDate:beginningOfWeek]; } //传入今天的时间,返回昨天的时间 + (NSString *)GetYesterdayDay:(NSDate *)aDate { NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; NSDateComponents *components = [gregorian components:NSCalendarUnitWeekday | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:aDate]; [components setDay:([components day]-1)]; NSDate *beginningOfWeek = [gregorian dateFromComponents:components]; NSDateFormatter *dateday = [[NSDateFormatter alloc] init]; [dateday setDateFormat:@"yyyy-MM-dd"]; return [dateday stringFromDate:beginningOfWeek]; } //字符串转时间戳 + (NSTimeInterval)timeStrToInterval:(NSString *)timeStr { NSDateFormatter *formate = [[NSDateFormatter alloc] init]; [formate setTimeZone:[NSTimeZone localTimeZone]]; [formate setDateFormat:@"yyyy-MM-dd HH:mm"]; NSDate *date = [formate dateFromString:timeStr]; NSTimeInterval dateInterval = [date timeIntervalSince1970]; return dateInterval; } @end