NSDate+RQExtension.m 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. //
  2. // NSDate+RQExtension.m
  3. // RQCommon
  4. //
  5. // Created by 张嵘 on 2018/11/23.
  6. // Copyright © 2018 张嵘. All rights reserved.
  7. //
  8. #import "NSDate+RQExtension.h"
  9. #define DATE_COMPONENTS (NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond | NSCalendarUnitWeekOfMonth | NSCalendarUnitWeekOfYear | NSCalendarUnitWeekday | NSCalendarUnitWeekdayOrdinal)
  10. #define CURRENT_CALENDAR [NSCalendar currentCalendar]
  11. @implementation NSDate (RQExtension)
  12. /**
  13. * 是否为今天
  14. */
  15. - (BOOL)rq_isToday
  16. {
  17. NSCalendar *calendar = [NSCalendar currentCalendar];
  18. int unit = NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear;
  19. // 1.获得当前时间的年月日
  20. NSDateComponents *nowCmps = [calendar components:unit fromDate:[NSDate date]];
  21. // 2.获得self的年月日
  22. NSDateComponents *selfCmps = [calendar components:unit fromDate:self];
  23. return
  24. (selfCmps.year == nowCmps.year) &&
  25. (selfCmps.month == nowCmps.month) &&
  26. (selfCmps.day == nowCmps.day);
  27. }
  28. /**
  29. * 是否为昨天
  30. */
  31. - (BOOL)rq_isYesterday
  32. {
  33. // 2014-05-01
  34. NSDate *nowDate = [[NSDate date] rq_dateWithYMD];
  35. // 2014-04-30
  36. NSDate *selfDate = [self rq_dateWithYMD];
  37. // 获得nowDate和selfDate的差距
  38. NSCalendar *calendar = [NSCalendar currentCalendar];
  39. NSDateComponents *cmps = [calendar components:NSCalendarUnitDay fromDate:selfDate toDate:nowDate options:0];
  40. return cmps.day == 1;
  41. }
  42. - (NSDate *)rq_dateWithYMD
  43. {
  44. NSDateFormatter *fmt = [NSDateFormatter rq_defaultDateFormatter];
  45. fmt.dateFormat = @"yyyy-MM-dd";
  46. NSString *selfStr = [fmt stringFromDate:self];
  47. return [fmt dateFromString:selfStr];
  48. }
  49. /**
  50. * 是否为今年
  51. */
  52. - (BOOL)rq_isThisYear
  53. {
  54. NSCalendar *calendar = [NSCalendar currentCalendar];
  55. int unit = NSCalendarUnitYear;
  56. // 1.获得当前时间的年月日
  57. NSDateComponents *nowCmps = [calendar components:unit fromDate:[NSDate date]];
  58. // 2.获得self的年月日
  59. NSDateComponents *selfCmps = [calendar components:unit fromDate:self];
  60. return nowCmps.year == selfCmps.year;
  61. }
  62. // This hard codes the assumption that a week is 7 days
  63. - (BOOL) rq_isSameWeekWithAnotherDate: (NSDate *)anotherDate
  64. {
  65. NSDateComponents *components1 = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self];
  66. NSDateComponents *components2 = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:anotherDate];
  67. // Must be same week. 12/31 and 1/1 will both be week "1" if they are in the same week
  68. if (components1.weekOfMonth != components2.weekOfMonth) return NO;
  69. // Must have a time interval under 1 week. Thanks @aclark
  70. return (fabs([self timeIntervalSinceDate:anotherDate]) < RQ_D_WEEK);
  71. }
  72. /**
  73. * 星期几
  74. */
  75. - (NSString *)rq_weekDay
  76. {
  77. NSArray *weekDays = @[@"星期日",@"星期一",@"星期二",@"星期三",@"星期四",@"星期五",@"星期六"];
  78. NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self];
  79. // NSLog(@"NSCalendarUnitWeekday-- components.weekday = %zd -- components.weekdayOrdinal = %zd -- components.weekOfYear = %zd -- components.weekOfMonth = %zd",components.weekday , components.weekdayOrdinal , components.weekOfYear , components.weekOfMonth);
  80. return weekDays[components.weekday - 1];
  81. }
  82. - (BOOL) rq_isThisWeek
  83. {
  84. return [self rq_isSameWeekWithAnotherDate:[NSDate date]];
  85. }
  86. /**
  87. * 通过一个时间 固定的时间字符串 "2016-8-10 14:43:45" 返回时间
  88. *
  89. * @param timestamp 固定的时间字符串 "2016-8-10 14:43:45"
  90. *
  91. * @return 时间
  92. */
  93. + (instancetype)rq_dateWithTimestamp:(NSString *)timestamp
  94. {
  95. NSDateFormatter *dateFormatter = [NSDateFormatter rq_defaultDateFormatter];
  96. [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
  97. return [dateFormatter dateFromString:timestamp];
  98. }
  99. /**
  100. * 返回固定的 当前时间 2016-8-10 14:43:45
  101. */
  102. + (NSString *)rq_currentTimestamp
  103. {
  104. return [[NSDateFormatter rq_defaultDateFormatter] stringFromDate:[NSDate date]];
  105. }
  106. /**
  107. * 格式化日期描述
  108. */
  109. - (NSString *)rq_formattedDateDescription
  110. {
  111. NSDateFormatter *dateFormatter = nil;
  112. NSString *result = nil;
  113. if ([self rq_isThisYear])
  114. {
  115. // 今年
  116. if ([self rq_isToday]) {
  117. // 22:22
  118. dateFormatter = [NSDateFormatter rq_dateFormatterWithFormat:@"HH:mm"];
  119. }else if ([self rq_isYesterday]){
  120. // 昨天 22:22
  121. dateFormatter = [NSDateFormatter rq_dateFormatterWithFormat:@"昨天 HH:mm"];
  122. }else if ([self rq_isThisWeek]){
  123. // 星期二 22:22
  124. dateFormatter = [NSDateFormatter rq_dateFormatterWithFormat:[NSString stringWithFormat:@"%@ HH:mm" , [self rq_weekDay]]];
  125. }else{
  126. // 2016年08月18日 22:22
  127. dateFormatter = [NSDateFormatter rq_dateFormatterWithFormat:@"yyyy年MM月dd日 HH:mm"];
  128. }
  129. }else{
  130. // 非今年
  131. dateFormatter = [NSDateFormatter rq_dateFormatterWithFormat:@"yyyy年MM月dd日"];
  132. }
  133. result = [dateFormatter stringFromDate:self];
  134. return result;
  135. }
  136. /** 与当前时间的差距 */
  137. - (NSDateComponents *)rq_deltaWithNow
  138. {
  139. NSCalendar *calendar = [NSCalendar currentCalendar];
  140. int unit = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
  141. return [calendar components:unit fromDate:self toDate:[NSDate date] options:0];
  142. }
  143. //////////// MVC&MVVM的商品的发布时间的描述 ////////////
  144. - (NSString *)rq_string_yyyy_MM_dd {
  145. return [self rq_string_yyyy_MM_dd:[NSDate date]];
  146. }
  147. - (NSString *)rq_string_yyyy_MM_dd:(NSDate *)toDate {
  148. // 设置日期格式(声明字符串里面每个数字和单词的含义)
  149. // E:星期几
  150. // M:月份
  151. // d:几号(这个月的第几天)
  152. // H:24小时制的小时
  153. // m:分钟
  154. // s:秒
  155. // y:年
  156. // fmt.dateFormat = @"EEE MMM dd HH:mm:ss Z yyyy";
  157. NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
  158. // 真机调试,转换这种欧美时间,需要设置locale
  159. fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
  160. // fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
  161. // fmt.locale = [NSLocale currentLocale];
  162. // 由于小闲肉使用的是北京时间,需要减掉8小时时差(转为欧美时间体系)
  163. NSDate *fromDate = [NSDate dateWithTimeIntervalSince1970:(self.timeIntervalSince1970 - 8*60*60)];
  164. // 日历对象(方便比较两个日期之间的差距)
  165. NSCalendar *calendar = [NSCalendar currentCalendar];
  166. // NSCalendarUnit枚举代表想获得哪些差值
  167. NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
  168. // 计算两个日期之间的差值
  169. NSDateComponents *cmps = [calendar components:unit fromDate:fromDate toDate:toDate options:0];
  170. if (cmps.year == 0) { // 今年
  171. if (cmps.year == 0 && cmps.month == 0 && cmps.day == 1) { // 昨天
  172. // fmt.dateFormat = @"昨天 HH:mm";
  173. fmt.dateFormat = @"MM-dd HH:mm";
  174. return [fmt stringFromDate:fromDate];
  175. } else if (cmps.year == 0 && cmps.month == 0 && cmps.day == 0) { // 今天
  176. if (cmps.hour >= 1) {
  177. return [NSString stringWithFormat:@"%d小时前", (int)cmps.hour];
  178. } else if (cmps.minute >= 1) {
  179. return [NSString stringWithFormat:@"%d分钟前", (int)cmps.minute];
  180. } else {
  181. return @"刚刚";
  182. }
  183. } else { // 今年的其他日子
  184. fmt.dateFormat = @"MM-dd HH:mm";
  185. return [fmt stringFromDate:fromDate];
  186. }
  187. } else { // 非今年
  188. fmt.dateFormat = @"yyyy-MM-dd HH:mm";
  189. return [fmt stringFromDate:fromDate];
  190. }
  191. }
  192. //////////// MVC&MVVM的商品的发布时间的描述 ////////////
  193. @end