《省钱达人》与《猎豆优选》UI相同版。域名tbk

DateManager.m 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //
  2. // DateManager.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/7/9.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "DateManager.h"
  9. #import "DateModel.h"
  10. @implementation DateManager
  11. + (NSArray *)getBuyLimitDateModels {
  12. NSMutableArray *array = [self getBuyLimitTimestrArr];
  13. NSDateFormatter *formate = [[NSDateFormatter alloc] init];
  14. [formate setTimeZone:[NSTimeZone localTimeZone]];
  15. [formate setDateFormat:@"yyyy-MM-dd HH:mm"];
  16. for (int i = 0; i < array.count; i++) {
  17. DateModel *model = array[i];
  18. NSDate *startDate = [formate dateFromString:model.startDateStr];
  19. NSTimeInterval startDateInterval = [startDate timeIntervalSince1970];
  20. NSDate *endDate = [formate dateFromString:model.endDateStr];
  21. NSTimeInterval endDateInterval = [endDate timeIntervalSince1970];
  22. NSDate *date = [NSDate date];
  23. NSTimeInterval localeDateInterval = [date timeIntervalSince1970];
  24. if (localeDateInterval >= startDateInterval && localeDateInterval <= endDateInterval) {
  25. return [self subModelFromModelArr:array index:i];
  26. }
  27. }
  28. return [self subModelFromModelArr:array index:0];
  29. }
  30. + (NSArray *)subModelFromModelArr:(NSMutableArray *)totalArr index:(NSInteger)index{
  31. NSInteger local = index-7>0 ? index-7:0;
  32. NSArray *dateArr = [totalArr subarrayWithRange:NSMakeRange(local, 15)];
  33. return dateArr;
  34. }
  35. + (NSMutableArray *)getBuyLimitTimestrArr {
  36. NSString *curDateStr = [self getCurrentTime];
  37. NSString *yesDateStr = [self GetYesterdayDay:[NSDate date]];
  38. NSString *tomDateStr = [self GetTomorrowDay:[NSDate date]];
  39. 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"];
  40. NSMutableArray *yesTimeArr = [NSMutableArray array];
  41. NSMutableArray *todTimeArr = [NSMutableArray array];
  42. NSMutableArray *tomTimeArr = [NSMutableArray array];
  43. for (NSString *timeStr in timeArr) {
  44. NSString *yesTimeStr = [NSString stringWithFormat:@"%@ %@",yesDateStr,timeStr];
  45. NSString *todTimeStr = [NSString stringWithFormat:@"%@ %@",curDateStr,timeStr];
  46. NSString *tomTimeStr = [NSString stringWithFormat:@"%@ %@",tomDateStr,timeStr];
  47. DateModel *yesModel = [[DateModel alloc] init];
  48. yesModel.endTimeStr = timeStr;
  49. yesModel.endDateStr = yesTimeStr;
  50. DateModel *todModel = [[DateModel alloc] init];
  51. todModel.endTimeStr = timeStr;
  52. todModel.endDateStr = todTimeStr;
  53. DateModel *tomModel = [[DateModel alloc] init];
  54. tomModel.endTimeStr = timeStr;
  55. tomModel.endDateStr = tomTimeStr;
  56. [yesTimeArr addObject:yesModel];
  57. [todTimeArr addObject:todModel];
  58. [tomTimeArr addObject:tomModel];
  59. }
  60. NSMutableArray *totalArr = [NSMutableArray array];
  61. [totalArr addObjectsFromArray:yesTimeArr];
  62. [totalArr addObjectsFromArray:todTimeArr];
  63. [totalArr addObjectsFromArray:tomTimeArr];
  64. for (int i = 0; i < totalArr.count; i++) {
  65. DateModel *model = totalArr[i];
  66. if (i==0) {
  67. model.startTimeStr = @"23:00";
  68. model.startDateStr = [NSString stringWithFormat:@"%@ %@",curDateStr,@"23:00"];
  69. }else if (i==totalArr.count-1){
  70. model.startTimeStr = @"00:00";
  71. model.startDateStr = [NSString stringWithFormat:@"%@ %@",curDateStr,@"00:00"];
  72. }else {
  73. DateModel *lastModel = totalArr[i-1];
  74. model.startTimeStr = lastModel.endTimeStr;
  75. model.startDateStr = lastModel.endDateStr;
  76. }
  77. }
  78. return totalArr;
  79. }
  80. //获取当地时间
  81. + (NSString *)getCurrentTime {
  82. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  83. [formatter setDateFormat:@"yyyy-MM-dd"];
  84. NSString *dateTime = [formatter stringFromDate:[NSDate date]];
  85. return dateTime;
  86. }
  87. //将字符串转成NSDate类型
  88. + (NSDate *)dateFromString:(NSString *)dateString {
  89. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  90. [dateFormatter setDateFormat: @"yyyy-MM-dd"];
  91. NSDate *destDate= [dateFormatter dateFromString:dateString];
  92. return destDate;
  93. }
  94. //传入今天的时间,返回明天的时间
  95. + (NSString *)GetTomorrowDay:(NSDate *)aDate {
  96. NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
  97. NSDateComponents *components = [gregorian components:NSCalendarUnitWeekday | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:aDate];
  98. [components setDay:([components day]+1)];
  99. NSDate *beginningOfWeek = [gregorian dateFromComponents:components];
  100. NSDateFormatter *dateday = [[NSDateFormatter alloc] init];
  101. [dateday setDateFormat:@"yyyy-MM-dd"];
  102. return [dateday stringFromDate:beginningOfWeek];
  103. }
  104. //传入今天的时间,返回昨天的时间
  105. + (NSString *)GetYesterdayDay:(NSDate *)aDate {
  106. NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
  107. NSDateComponents *components = [gregorian components:NSCalendarUnitWeekday | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:aDate];
  108. [components setDay:([components day]-1)];
  109. NSDate *beginningOfWeek = [gregorian dateFromComponents:components];
  110. NSDateFormatter *dateday = [[NSDateFormatter alloc] init];
  111. [dateday setDateFormat:@"yyyy-MM-dd"];
  112. return [dateday stringFromDate:beginningOfWeek];
  113. }
  114. //字符串转时间戳
  115. + (NSTimeInterval)timeStrToInterval:(NSString *)timeStr {
  116. NSDateFormatter *formate = [[NSDateFormatter alloc] init];
  117. [formate setTimeZone:[NSTimeZone localTimeZone]];
  118. [formate setDateFormat:@"yyyy-MM-dd HH:mm"];
  119. NSDate *date = [formate dateFromString:timeStr];
  120. NSTimeInterval dateInterval = [date timeIntervalSince1970];
  121. return dateInterval;
  122. }
  123. @end