NSDate+Extension.m 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. //
  2. // NSDate+Extension.m
  3. // YZCCalender
  4. //
  5. // Created by Jason on 2018/1/17.
  6. // Copyright © 2018年 jason. All rights reserved.
  7. //
  8. #import "NSDate+Extension.h"
  9. @implementation NSDate (Extension)
  10. #pragma mark -- 获取日
  11. + (NSInteger)day:(NSString *)date {
  12. NSDateFormatter *dateFormatter = [[self class] setDataFormatter];
  13. NSDate *startDate = [dateFormatter dateFromString:date];
  14. NSDateComponents *components = [[NSCalendar currentCalendar] components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:startDate];
  15. return components.day;
  16. }
  17. #pragma mark -- 获取月
  18. + (NSInteger)month:(NSString *)date {
  19. NSDateFormatter *dateFormatter = [[self class] setDataFormatter];
  20. NSDate *startDate = [dateFormatter dateFromString:date];
  21. NSDateComponents *components = [[NSCalendar currentCalendar] components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:startDate];
  22. return components.month;
  23. }
  24. #pragma mark -- 获取年
  25. + (NSInteger)year:(NSString *)date {
  26. NSDateFormatter *dateFormatter = [[self class] setDataFormatter];
  27. NSDate *startDate = [dateFormatter dateFromString:date];
  28. NSDateComponents *components = [[NSCalendar currentCalendar] components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:startDate];
  29. return components.year;
  30. }
  31. #pragma mark -- 获得当前月份第一天星期几
  32. + (NSInteger)firstWeekdayInThisMonth:(NSDate *)date {
  33. NSCalendar *calendar = [NSCalendar currentCalendar];
  34. //设置每周的第一天从周几开始,默认为1,从周日开始
  35. [calendar setFirstWeekday:1];//1.Sun. 2.Mon. 3.Thes. 4.Wed. 5.Thur. 6.Fri. 7.Sat.
  36. NSDateComponents *comp = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:date];
  37. [comp setDay:1];
  38. NSDate *firstDayOfMonthDate = [calendar dateFromComponents:comp];
  39. NSUInteger firstWeekday = [calendar ordinalityOfUnit:NSCalendarUnitWeekday inUnit:NSCalendarUnitWeekOfMonth forDate:firstDayOfMonthDate];
  40. //若设置从周日开始算起则需要减一,若从周一开始算起则不需要减
  41. return firstWeekday - 1;
  42. }
  43. #pragma mark -- 获取当前月共有多少天
  44. + (NSInteger)totaldaysInMonth:(NSDate *)date {
  45. NSRange daysInLastMonth = [[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date];
  46. return daysInLastMonth.length;
  47. }
  48. #pragma mark -- 获取日期
  49. + (NSString *)timeStringWithInterval:(NSTimeInterval)timeInterval {
  50. NSDateFormatter *dateFormatter = [[self class] setDataFormatter];
  51. NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeInterval];
  52. NSString *dateString = [dateFormatter stringFromDate:date];
  53. return dateString;
  54. }
  55. #pragma mark -- 设置日期格式
  56. + (NSDateFormatter *)setDataFormatter {
  57. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  58. [dateFormatter setDateFormat:@"yyyy-MM-dd"];
  59. [dateFormatter setLocale:[NSLocale currentLocale]];
  60. return dateFormatter;
  61. }
  62. #pragma mark -- 计算两个日期之间相差天数
  63. + (NSDateComponents *)calcDaysbetweenDate:(NSString *)startDateStr endDateStr:(NSString *)endDateStr {
  64. NSDateFormatter *dateFormatter = [[self class] setDataFormatter];
  65. NSDate *startDate = [dateFormatter dateFromString:startDateStr];
  66. NSDate *endDate = [dateFormatter dateFromString:endDateStr];
  67. //利用NSCalendar比较日期的差异
  68. NSCalendar *calendar = [NSCalendar currentCalendar];
  69. /**
  70. * 要比较的时间单位,常用如下,可以同时传:
  71. * NSCalendarUnitDay : 天
  72. * NSCalendarUnitYear : 年
  73. * NSCalendarUnitMonth : 月
  74. * NSCalendarUnitHour : 时
  75. * NSCalendarUnitMinute : 分
  76. * NSCalendarUnitSecond : 秒
  77. */
  78. NSCalendarUnit unit = NSCalendarUnitDay | NSCalendarUnitMonth;
  79. NSDateComponents *delta = [calendar components:unit fromDate:startDate toDate:endDate options:0];
  80. return delta;
  81. }
  82. + (NSTimeInterval)timeIntervalFromDateString:(NSString *)dateString {
  83. static NSDateFormatter *dateFormatter = nil;
  84. if (dateFormatter == nil) {
  85. dateFormatter = [[NSDateFormatter alloc] init];
  86. [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
  87. [dateFormatter setLocale:[NSLocale currentLocale]];
  88. }
  89. NSDate *date = [dateFormatter dateFromString:dateString];
  90. NSTimeInterval interval = [date timeIntervalSince1970];
  91. return interval;
  92. }
  93. + (BOOL)isToday:(NSString *)date {
  94. BOOL isToday = NO;
  95. NSString *today = [NSDate timeStringWithInterval:[NSDate date].timeIntervalSince1970];
  96. if ([date isEqualToString:today]) {
  97. isToday = YES;
  98. }
  99. return isToday;
  100. }
  101. + (BOOL)isEqualBetweenWithDate:(NSString *)date toDate:(NSString *)toDate {
  102. BOOL isToday = NO;
  103. if ([toDate isEqualToString:date]) {
  104. isToday = YES;
  105. }
  106. return isToday;
  107. }
  108. + (BOOL)isCurrenMonth:(NSString *)date {
  109. BOOL isCurrenMonth = NO;
  110. NSString *month = [[NSDate timeStringWithInterval:[NSDate date].timeIntervalSince1970] substringWithRange:NSMakeRange(0, 7)];
  111. if ([date isEqualToString:month]) {
  112. isCurrenMonth = YES;
  113. }
  114. return isCurrenMonth;
  115. }
  116. + (BOOL)isCurrenYear:(NSString *)date{
  117. BOOL isCurrenYear = NO;
  118. NSString *year = [[NSDate timeStringWithInterval:[NSDate date].timeIntervalSince1970] substringWithRange:NSMakeRange(0, 4)];
  119. if ([date isEqualToString:year]) {
  120. isCurrenYear = YES;
  121. }
  122. return isCurrenYear;
  123. }
  124. + (NSString *)getTimeWihtDate:(NSString *)date
  125. {
  126. // NSLog(@"日:%@",[date substringWithRange:NSMakeRange(11, 5)]);
  127. // NSLog(@"月:%@",[date substringWithRange:NSMakeRange(5, 5)]);
  128. // NSLog(@"年:%@",[date substringWithRange:NSMakeRange(0, 10)]);
  129. // date = @"2017-11-10 09:11:11";
  130. if ([[date substringWithRange:NSMakeRange(0, 10)] isEqualToString:[NSDate timeStringWithInterval:[NSDate date].timeIntervalSince1970]]) {
  131. return [date substringWithRange:NSMakeRange(11, 5)];
  132. }else if([[date substringWithRange:NSMakeRange(0, 7)] isEqualToString:[[NSDate timeStringWithInterval:[NSDate date].timeIntervalSince1970] substringWithRange:NSMakeRange(0, 7)]]){
  133. return [date substringWithRange:NSMakeRange(5, 5)];
  134. }else{
  135. return [date substringWithRange:NSMakeRange(0, 10)];
  136. }
  137. }
  138. + (NSString *)getTimeStringAutoShort2:(NSString *)date{
  139. if ([date isKindOfClass:[NSNull class]] || date == nil) {
  140. return @"";
  141. }else{
  142. if (date.length == 0) {
  143. return @"";
  144. }
  145. }
  146. NSDate * dt = [self getStringDate:date format:@"yyyy-MM-dd HH:mm:ss"];
  147. NSString * ret = nil;
  148. NSCalendar * calendar = [NSCalendar currentCalendar];
  149. // 当前时间
  150. NSDate * currentDate = [NSDate date];
  151. NSDateComponents * curComponents = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitWeekday fromDate:currentDate];
  152. NSInteger currentYear = [curComponents year];
  153. NSInteger currentMonth = [curComponents month];
  154. NSInteger currentDay = [curComponents day];
  155. // 目标判断时间
  156. NSDateComponents*srcComponents = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitWeekday fromDate:dt];
  157. NSInteger srcYear = [srcComponents year];
  158. NSInteger srcMonth = [srcComponents month];
  159. NSInteger srcDay = [srcComponents day];
  160. // 要额外显示的时间分钟
  161. // 当年
  162. if(currentYear == srcYear) {
  163. long currentTimestamp = [self getIOSTimeStamp_l:currentDate];
  164. long srcTimestamp = [self getIOSTimeStamp_l:dt];
  165. // 相差时间(单位:秒)
  166. long delta = currentTimestamp - srcTimestamp;
  167. // 当天(月份和日期一致才是)
  168. if(currentMonth == srcMonth && currentDay == srcDay) {
  169. // 时间相差60秒以内
  170. if(delta < 60){
  171. ret = @"刚刚";
  172. }else if (delta < 60 * 60){
  173. ret = [NSString stringWithFormat:@"%ld分钟前",(long)delta/60];
  174. }else{
  175. ret = [NSString stringWithFormat:@"%ld小时前",(long)delta/60/60];
  176. }
  177. }else{
  178. ret = [self getTimeString:dt format:@"MM-dd HH:mm"];
  179. }
  180. }else{/// 往年 2020-12-21
  181. ret = [self getTimeString:dt format:@"yyyy-MM-dd"];
  182. }
  183. return ret;
  184. }
  185. + (NSString *)getTimeString:(NSDate *)dt format:(NSString *)fmt{
  186. NSDateFormatter* format = [[NSDateFormatter alloc] init];
  187. [format setDateFormat:fmt];
  188. return [format stringFromDate:dt];
  189. }
  190. + (NSDate *)getStringDate:(NSString *)date format:(NSString *)fmt{
  191. NSDateFormatter* format = [[NSDateFormatter alloc] init];
  192. [format setDateFormat:fmt];
  193. return [format dateFromString:date];
  194. }
  195. + (NSTimeInterval)getIOSTimeStamp:(NSDate*)dat{
  196. NSTimeInterval a = [dat timeIntervalSince1970];
  197. return a;
  198. }
  199. + (long)getIOSTimeStamp_l:(NSDate*)dat{
  200. return[[NSNumber numberWithDouble:[self getIOSTimeStamp:dat]] longValue];
  201. }
  202. + (NSString *)getCHtimeWihtDate:(NSString *)date
  203. {
  204. NSDate * dt = [self getStringDate:date format:@"yyyy-MM-dd HH:mm:ss"];
  205. NSString * ret = nil;
  206. NSCalendar * calendar = [NSCalendar currentCalendar];
  207. // 当前时间
  208. NSDate * currentDate = [NSDate date];
  209. NSDateComponents * curComponents = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitWeekday fromDate:currentDate];
  210. NSInteger currentYear = [curComponents year];
  211. // 目标判断时间
  212. NSDateComponents*srcComponents = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitWeekday fromDate:dt];
  213. NSInteger srcYear = [srcComponents year];
  214. // 要额外显示的时间分钟
  215. // 当年
  216. if(currentYear == srcYear) {
  217. ret = [self getTimeString:dt format:@"MM-dd HH:mm"];
  218. }else{/// 往年 2020-12-21
  219. ret = [self getTimeString:dt format:@"yyyy-MM-dd HH:mm"];
  220. }
  221. return ret;
  222. }
  223. @end