EMDateHelper.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. //
  2. // EMDateHelper.m
  3. // ChatDemo-UI3.0
  4. //
  5. // Created by XieYajie on 2019/2/12.
  6. // Copyright © 2019 XieYajie. All rights reserved.
  7. //
  8. #import "EMDateHelper.h"
  9. #define DATE_COMPONENTS (NSCalendarUnitYear| NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekOfYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond | NSCalendarUnitWeekday | NSCalendarUnitWeekdayOrdinal)
  10. #define CURRENT_CALENDAR [NSCalendar currentCalendar]
  11. @interface EMDateHelper()
  12. @property (nonatomic, strong) NSDateFormatter *dfYMD;
  13. @property (nonatomic, strong) NSDateFormatter *dfHM;
  14. @property (nonatomic, strong) NSDateFormatter *dfYMDHM;
  15. @property (nonatomic, strong) NSDateFormatter *dfYesterdayHM;
  16. @property (nonatomic, strong) NSDateFormatter *dfBeforeDawnHM;
  17. @property (nonatomic, strong) NSDateFormatter *dfAAHM;
  18. @property (nonatomic, strong) NSDateFormatter *dfPPHM;
  19. @property (nonatomic, strong) NSDateFormatter *dfNightHM;
  20. @end
  21. static EMDateHelper *shared = nil;
  22. @implementation EMDateHelper
  23. + (instancetype)shareHelper
  24. {
  25. static dispatch_once_t onceToken;
  26. dispatch_once(&onceToken, ^{
  27. shared = [[EMDateHelper alloc] init];
  28. });
  29. return shared;
  30. }
  31. - (instancetype)init
  32. {
  33. self = [super init];
  34. if (self) {
  35. //
  36. }
  37. return self;
  38. }
  39. #pragma mark - Getter
  40. - (NSDateFormatter *)_getDateFormatterWithFormat:(NSString *)aFormat
  41. {
  42. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  43. dateFormatter.dateFormat = aFormat;
  44. return dateFormatter;
  45. }
  46. - (NSDateFormatter *)dfYMD
  47. {
  48. if (_dfYMD == nil) {
  49. _dfYMD = [self _getDateFormatterWithFormat:@"YYYY/MM/dd"];
  50. }
  51. return _dfYMD;
  52. }
  53. - (NSDateFormatter *)dfHM
  54. {
  55. if (_dfHM == nil) {
  56. _dfHM = [self _getDateFormatterWithFormat:@"HH:mm"];
  57. }
  58. return _dfHM;
  59. }
  60. - (NSDateFormatter *)dfYMDHM
  61. {
  62. if (_dfYMDHM == nil) {
  63. _dfYMDHM = [self _getDateFormatterWithFormat:@"yyyy/MM/dd HH:mm"];
  64. }
  65. return _dfYMDHM;
  66. }
  67. - (NSDateFormatter *)dfYesterdayHM
  68. {
  69. if (_dfYesterdayHM == nil) {
  70. _dfYesterdayHM = [self _getDateFormatterWithFormat:@"昨天HH:mm"];
  71. }
  72. return _dfYesterdayHM;
  73. }
  74. - (NSDateFormatter *)dfBeforeDawnHM
  75. {
  76. if (_dfBeforeDawnHM == nil) {
  77. _dfBeforeDawnHM = [self _getDateFormatterWithFormat:@"凌晨hh:mm"];
  78. }
  79. return _dfBeforeDawnHM;
  80. }
  81. - (NSDateFormatter *)dfAAHM
  82. {
  83. if (_dfAAHM == nil) {
  84. _dfAAHM = [self _getDateFormatterWithFormat:@"上午hh:mm"];
  85. }
  86. return _dfAAHM;
  87. }
  88. - (NSDateFormatter *)dfPPHM
  89. {
  90. if (_dfPPHM == nil) {
  91. _dfPPHM = [self _getDateFormatterWithFormat:@"下午hh:mm"];
  92. }
  93. return _dfPPHM;
  94. }
  95. - (NSDateFormatter *)dfNightHM
  96. {
  97. if (_dfNightHM == nil) {
  98. _dfNightHM = [self _getDateFormatterWithFormat:@"晚上hh:mm"];
  99. }
  100. return _dfNightHM;
  101. }
  102. #pragma mark - Class Methods
  103. + (NSDate *)dateWithTimeIntervalInMilliSecondSince1970:(double)aMilliSecond
  104. {
  105. double timeInterval = aMilliSecond;
  106. // judge if the argument is in secconds(for former data structure).
  107. if(aMilliSecond > 140000000000) {
  108. timeInterval = aMilliSecond / 1000;
  109. }
  110. NSDate *ret = [NSDate dateWithTimeIntervalSince1970:timeInterval];
  111. return ret;
  112. }
  113. + (NSString *)formattedTimeFromTimeInterval:(long long)aTimeInterval
  114. {
  115. NSDate *date = [EMDateHelper dateWithTimeIntervalInMilliSecondSince1970:aTimeInterval];
  116. return [EMDateHelper formattedTime:date];
  117. }
  118. + (NSString *)formattedTime:(NSDate *)aDate
  119. {
  120. EMDateHelper *helper = [EMDateHelper shareHelper];
  121. NSString *dateNow = [helper.dfYMD stringFromDate:[NSDate date]];
  122. NSDateComponents *components = [[NSDateComponents alloc] init];
  123. [components setDay:[[dateNow substringWithRange:NSMakeRange(8, 2)] intValue]];
  124. [components setMonth:[[dateNow substringWithRange:NSMakeRange(5, 2)] intValue]];
  125. [components setYear:[[dateNow substringWithRange:NSMakeRange(0, 4)] intValue]];
  126. NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
  127. NSDate *date = [gregorian dateFromComponents:components];
  128. NSInteger hour = [EMDateHelper hoursFromDate:aDate toDate:date];
  129. NSDateFormatter *dateFormatter = nil;
  130. NSString *ret = @"";
  131. //If hasAMPM==TURE, use 12-hour clock, otherwise use 24-hour clock
  132. NSString *formatStringForHours = [NSDateFormatter dateFormatFromTemplate:@"j" options:0 locale:[NSLocale currentLocale]];
  133. NSRange containsA = [formatStringForHours rangeOfString:@"a"];
  134. BOOL hasAMPM = containsA.location != NSNotFound;
  135. if (!hasAMPM) { //24-hour clock
  136. if (hour <= 24 && hour >= 0) {
  137. dateFormatter = helper.dfHM;
  138. } else if (hour < 0 && hour >= -24) {
  139. dateFormatter = helper.dfYesterdayHM;
  140. } else {
  141. dateFormatter = helper.dfYMDHM;
  142. }
  143. } else {
  144. if (hour >= 0 && hour <= 6) {
  145. dateFormatter = helper.dfBeforeDawnHM;
  146. } else if (hour > 6 && hour <= 11 ) {
  147. dateFormatter = helper.dfAAHM;
  148. } else if (hour > 11 && hour <= 17) {
  149. dateFormatter = helper.dfPPHM;
  150. } else if (hour > 17 && hour <= 24) {
  151. dateFormatter = helper.dfNightHM;
  152. } else if (hour < 0 && hour >= -24) {
  153. dateFormatter = helper.dfYesterdayHM;
  154. } else {
  155. dateFormatter = helper.dfYMDHM;
  156. }
  157. }
  158. ret = [dateFormatter stringFromDate:aDate];
  159. return ret;
  160. }
  161. #pragma mark Retrieving Intervals
  162. + (NSInteger)hoursFromDate:(NSDate *)aFromDate
  163. toDate:(NSDate *)aToDate
  164. {
  165. NSTimeInterval ti = [aFromDate timeIntervalSinceDate:aToDate];
  166. return (NSInteger) (ti / D_HOUR);
  167. }
  168. @end