NSDate+Extension.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. // NSCalendarUnit type = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
  163. //
  164. // NSDateComponents *cmps = [calendar components:type fromDate:currentDate toDate:dt options:0];
  165. // // 4.输出结果
  166. // NSLog(@"两个时间相差%ld年%ld月%ld日%ld小时%ld分钟%ld秒", cmps.year, cmps.month, cmps.day, cmps.hour, cmps.minute, cmps.second);
  167. if(currentYear == srcYear) {
  168. long currentTimestamp = [self getIOSTimeStamp:currentDate];
  169. long srcTimestamp = [self getIOSTimeStamp:dt];
  170. // 相差时间(单位:秒)
  171. long delta = currentTimestamp - srcTimestamp;
  172. // 当天(月份和日期一致才是)
  173. if(currentMonth == srcMonth && currentDay == srcDay) {
  174. // 时间相差60秒以内
  175. if(delta < 60){
  176. ret = @"刚刚";
  177. }else if (delta < 60 * 60){
  178. ret = [NSString stringWithFormat:@"%ld分钟前",(long)delta/60];
  179. }else{
  180. ret = [NSString stringWithFormat:@"%ld小时前",(long)delta/60/60];
  181. }
  182. }else{
  183. ret = [self getTimeString:dt format:@"MM-dd HH:mm"];
  184. }
  185. }else{/// 往年 2020-12-21
  186. ret = [self getTimeString:dt format:@"yyyy-MM-dd"];
  187. }
  188. return ret;
  189. }
  190. + (NSString *)getTimeStringAutoShort3:(NSString *)date{
  191. if ([date isKindOfClass:[NSNull class]] || date == nil) {
  192. return @"";
  193. }else{
  194. if (date.length == 0) {
  195. return @"";
  196. }
  197. }
  198. NSDate * dt = [self getStringDate:date format:@"yyyy-MM-dd HH:mm:ss"];
  199. NSString * ret = [self getTimeString:dt format:@"yyyy-MM-dd"];
  200. return ret;
  201. }
  202. + (NSString *)getTimeString:(NSDate *)dt format:(NSString *)fmt{
  203. NSDateFormatter* format = [[NSDateFormatter alloc] init];
  204. [format setDateFormat:fmt];
  205. return [format stringFromDate:[NSDate dateWithTimeIntervalSince1970:[dt timeIntervalSince1970]]];
  206. }
  207. + (NSDate *)getStringDate:(NSString *)date format:(NSString *)fmt{
  208. NSDateFormatter* format = [[NSDateFormatter alloc] init];
  209. [format setDateFormat:fmt];
  210. return [format dateFromString:date];
  211. }
  212. + (long)getIOSTimeStamp:(NSDate*)dat{
  213. long a = [dat timeIntervalSince1970];
  214. return a;
  215. }
  216. + (long)getIOSTimeStamp_l:(NSDate*)dat{
  217. return[[NSNumber numberWithDouble:[self getIOSTimeStamp:dat]] longValue];
  218. }
  219. + (NSString *)getCHtimeWihtDate:(NSString *)date
  220. {
  221. NSDate * dt = [self getStringDate:date format:@"yyyy-MM-dd HH:mm:ss"];
  222. NSString * ret = nil;
  223. NSCalendar * calendar = [NSCalendar currentCalendar];
  224. // 当前时间
  225. NSDate * currentDate = [NSDate date];
  226. NSDateComponents * curComponents = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitWeekday fromDate:currentDate];
  227. NSInteger currentYear = [curComponents year];
  228. // 目标判断时间
  229. NSDateComponents*srcComponents = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitWeekday fromDate:dt];
  230. NSInteger srcYear = [srcComponents year];
  231. // 要额外显示的时间分钟
  232. // 当年
  233. if(currentYear == srcYear) {
  234. ret = [self getTimeString:dt format:@"MM-dd HH:mm"];
  235. }else{/// 往年 2020-12-21
  236. ret = [self getTimeString:dt format:@"yyyy-MM-dd HH:mm"];
  237. }
  238. return ret;
  239. }
  240. + (NSString *)getMonthAndDayWihtDate:(NSString *)date
  241. {
  242. NSDate * dt = [self getStringDate:date format:@"yyyy-MM-dd HH:mm:ss"];
  243. NSString * ret = [self getTimeString:dt format:@"MM-dd"];
  244. return ret;
  245. }
  246. @end