NSDate+RQExtension.m 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 alloc] init];
  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. return [[NSDateFormatter rq_defaultDateFormatter] dateFromString:timestamp];
  96. }
  97. + (NSString *)rq_timeIntervalWithTimestamp:(NSString *)timestamp
  98. {
  99. // 时间
  100. NSDate *date = [self rq_dateWithTimestamp:timestamp];
  101. // *1000 是精确到毫秒,不乘就是精确到秒
  102. NSTimeInterval time = [date timeIntervalSince1970];
  103. NSString *timeStr = [NSString stringWithFormat:@"%.0f", time];
  104. return timeStr;
  105. }
  106. /**
  107. * 返回固定的 当前时间 2016-8-10 14:43:45
  108. */
  109. + (NSString *)rq_currentTimestamp
  110. {
  111. return [[NSDateFormatter rq_defaultDateFormatter] stringFromDate:[NSDate date]];
  112. }
  113. + (NSString *)rq_currentTimeInterval
  114. {
  115. // 获取当前时间0秒后的时间
  116. NSDate *date = [NSDate dateWithTimeIntervalSinceNow:0];
  117. // *1000 是精确到毫秒,不乘就是精确到秒
  118. NSTimeInterval time = [date timeIntervalSince1970];
  119. NSString *timeStr = [NSString stringWithFormat:@"%.0f", time];
  120. return timeStr;
  121. }
  122. + (NSString *)rq_currentTimeSSSInterval
  123. {
  124. // 获取当前时间0秒后的时间
  125. NSDate *date = [NSDate dateWithTimeIntervalSinceNow:0];
  126. // *1000 是精确到毫秒,不乘就是精确到秒
  127. NSTimeInterval time = [date timeIntervalSince1970] * 1000;
  128. NSString *timeStr = [NSString stringWithFormat:@"%.0f", time];
  129. return timeStr;
  130. }
  131. /**
  132. * 格式化日期描述
  133. */
  134. - (NSString *)rq_formattedDateDescription
  135. {
  136. NSDateFormatter *dateFormatter = nil;
  137. NSString *result = nil;
  138. if ([self rq_isThisYear])
  139. {
  140. // 今年
  141. if ([self rq_isToday]) {
  142. // 22:22
  143. dateFormatter = [NSDateFormatter rq_dateFormatterWithFormat:@"HH:mm"];
  144. }else if ([self rq_isYesterday]){
  145. // 昨天 22:22
  146. dateFormatter = [NSDateFormatter rq_dateFormatterWithFormat:@"昨天 HH:mm"];
  147. }else if ([self rq_isThisWeek]){
  148. // 星期二 22:22
  149. dateFormatter = [NSDateFormatter rq_dateFormatterWithFormat:[NSString stringWithFormat:@"%@ HH:mm" , [self rq_weekDay]]];
  150. }else{
  151. // 2016年08月18日 22:22
  152. dateFormatter = [NSDateFormatter rq_dateFormatterWithFormat:@"yyyy年MM月dd日 HH:mm"];
  153. }
  154. }else{
  155. // 非今年
  156. dateFormatter = [NSDateFormatter rq_dateFormatterWithFormat:@"yyyy年MM月dd日"];
  157. }
  158. result = [dateFormatter stringFromDate:self];
  159. return result;
  160. }
  161. /** 与当前时间的差距 */
  162. - (NSDateComponents *)rq_deltaWithNow
  163. {
  164. NSCalendar *calendar = [NSCalendar currentCalendar];
  165. int unit = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
  166. return [calendar components:unit fromDate:self toDate:[NSDate date] options:0];
  167. }
  168. //////////// MVC&MVVM的商品的发布时间的描述 ////////////
  169. - (NSString *)rq_string_yyyy_MM_dd {
  170. return [self rq_string_yyyy_MM_dd:[NSDate date]];
  171. }
  172. - (NSString *)rq_string_yyyy_MM_dd:(NSDate *)toDate {
  173. // 设置日期格式(声明字符串里面每个数字和单词的含义)
  174. // E:星期几
  175. // M:月份
  176. // d:几号(这个月的第几天)
  177. // H:24小时制的小时
  178. // m:分钟
  179. // s:秒
  180. // y:年
  181. // fmt.dateFormat = @"EEE MMM dd HH:mm:ss Z yyyy";
  182. NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
  183. // 真机调试,转换这种欧美时间,需要设置locale
  184. fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
  185. // fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
  186. // fmt.locale = [NSLocale currentLocale];
  187. // 由于小闲肉使用的是北京时间,需要减掉8小时时差(转为欧美时间体系)
  188. NSDate *fromDate = [NSDate dateWithTimeIntervalSince1970:(self.timeIntervalSince1970 - 8*60*60)];
  189. // 日历对象(方便比较两个日期之间的差距)
  190. NSCalendar *calendar = [NSCalendar currentCalendar];
  191. // NSCalendarUnit枚举代表想获得哪些差值
  192. NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
  193. // 计算两个日期之间的差值
  194. NSDateComponents *cmps = [calendar components:unit fromDate:fromDate toDate:toDate options:0];
  195. if (cmps.year == 0) { // 今年
  196. if (cmps.year == 0 && cmps.month == 0 && cmps.day == 1) { // 昨天
  197. // fmt.dateFormat = @"昨天 HH:mm";
  198. fmt.dateFormat = @"MM-dd HH:mm";
  199. return [fmt stringFromDate:fromDate];
  200. } else if (cmps.year == 0 && cmps.month == 0 && cmps.day == 0) { // 今天
  201. if (cmps.hour >= 1) {
  202. return [NSString stringWithFormat:@"%d小时前", (int)cmps.hour];
  203. } else if (cmps.minute >= 1) {
  204. return [NSString stringWithFormat:@"%d分钟前", (int)cmps.minute];
  205. } else {
  206. return @"刚刚";
  207. }
  208. } else { // 今年的其他日子
  209. fmt.dateFormat = @"MM-dd HH:mm";
  210. return [fmt stringFromDate:fromDate];
  211. }
  212. } else { // 非今年
  213. fmt.dateFormat = @"yyyy-MM-dd HH:mm";
  214. return [fmt stringFromDate:fromDate];
  215. }
  216. }
  217. //////////// MVC&MVVM的商品的发布时间的描述 ////////////
  218. @end