NSDate+JKExtension.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. //
  2. // NSDate+Extension.h
  3. // JKCategories (https://github.com/shaojiankui/JKCategories)
  4. //
  5. // Created by Jakey on 15/4/25.
  6. // Copyright (c) 2015年 www.skyfox.org. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. @interface NSDate (JKExtension)
  10. /**
  11. * 获取日、月、年、小时、分钟、秒
  12. */
  13. - (NSUInteger)jk_day;
  14. - (NSUInteger)jk_month;
  15. - (NSUInteger)jk_year;
  16. - (NSUInteger)jk_hour;
  17. - (NSUInteger)jk_minute;
  18. - (NSUInteger)jk_second;
  19. + (NSUInteger)jk_day:(NSDate *)date;
  20. + (NSUInteger)jk_month:(NSDate *)date;
  21. + (NSUInteger)jk_year:(NSDate *)date;
  22. + (NSUInteger)jk_hour:(NSDate *)date;
  23. + (NSUInteger)jk_minute:(NSDate *)date;
  24. + (NSUInteger)jk_second:(NSDate *)date;
  25. /**
  26. * 获取一年中的总天数
  27. */
  28. - (NSUInteger)jk_daysInYear;
  29. + (NSUInteger)jk_daysInYear:(NSDate *)date;
  30. /**
  31. * 2个日期相差秒数
  32. */
  33. - (NSTimeInterval)by_timeIntervalWithDate:(NSDate *)date;
  34. /**
  35. * 判断是否是润年
  36. * @return YES表示润年,NO表示平年
  37. */
  38. - (BOOL)jk_isLeapYear;
  39. + (BOOL)jk_isLeapYear:(NSDate *)date;
  40. /**
  41. * 获取该日期是该年的第几周
  42. */
  43. - (NSUInteger)jk_weekOfYear;
  44. + (NSUInteger)jk_weekOfYear:(NSDate *)date;
  45. /**
  46. * 获取格式化为YYYY-MM-dd格式的日期字符串
  47. */
  48. - (NSString *)jk_formatYMD;
  49. + (NSString *)jk_formatYMD:(NSDate *)date;
  50. /**
  51. * 返回当前月一共有几周(可能为4,5,6)
  52. */
  53. - (NSUInteger)jk_weeksOfMonth;
  54. + (NSUInteger)jk_weeksOfMonth:(NSDate *)date;
  55. /**
  56. * 获取该月的第一天的日期
  57. */
  58. - (NSDate *)jk_begindayOfMonth;
  59. + (NSDate *)jk_begindayOfMonth:(NSDate *)date;
  60. /**
  61. * 获取该月的最后一天的日期
  62. */
  63. - (NSDate *)jk_lastdayOfMonth;
  64. + (NSDate *)jk_lastdayOfMonth:(NSDate *)date;
  65. /**
  66. * 返回day天后的日期(若day为负数,则为|day|天前的日期)
  67. */
  68. - (NSDate *)jk_dateAfterDay:(NSUInteger)day;
  69. + (NSDate *)jk_dateAfterDate:(NSDate *)date day:(NSInteger)day;
  70. /**
  71. * 返回day天后的日期(若day为负数,则为|day|天前的日期)
  72. */
  73. - (NSDate *)jk_dateAfterMonth:(NSUInteger)month;
  74. + (NSDate *)jk_dateAfterDate:(NSDate *)date month:(NSInteger)month;
  75. /**
  76. * 返回numYears年后的日期
  77. */
  78. - (NSDate *)jk_offsetYears:(int)numYears;
  79. + (NSDate *)jk_offsetYears:(int)numYears fromDate:(NSDate *)fromDate;
  80. /**
  81. * 返回numMonths月后的日期
  82. */
  83. - (NSDate *)jk_offsetMonths:(int)numMonths;
  84. + (NSDate *)jk_offsetMonths:(int)numMonths fromDate:(NSDate *)fromDate;
  85. /**
  86. * 返回numDays天后的日期
  87. */
  88. - (NSDate *)jk_offsetDays:(int)numDays;
  89. + (NSDate *)jk_offsetDays:(int)numDays fromDate:(NSDate *)fromDate;
  90. /**
  91. * 返回numHours小时后的日期
  92. */
  93. - (NSDate *)jk_offsetHours:(int)hours;
  94. + (NSDate *)jk_offsetHours:(int)numHours fromDate:(NSDate *)fromDate;
  95. /**
  96. * 距离该日期前几天
  97. */
  98. - (NSUInteger)jk_daysAgo;
  99. + (NSUInteger)jk_daysAgo:(NSDate *)date;
  100. /**
  101. * 获取星期几
  102. *
  103. * @return Return weekday number
  104. * [1 - Sunday]
  105. * [2 - Monday]
  106. * [3 - Tuerday]
  107. * [4 - Wednesday]
  108. * [5 - Thursday]
  109. * [6 - Friday]
  110. * [7 - Saturday]
  111. */
  112. - (NSInteger)jk_weekday;
  113. + (NSInteger)jk_weekday:(NSDate *)date;
  114. /**
  115. * 获取星期几(名称)
  116. *
  117. * @return Return weekday as a localized string
  118. * [1 - Sunday]
  119. * [2 - Monday]
  120. * [3 - Tuerday]
  121. * [4 - Wednesday]
  122. * [5 - Thursday]
  123. * [6 - Friday]
  124. * [7 - Saturday]
  125. */
  126. - (NSString *)jk_dayFromWeekday;
  127. + (NSString *)jk_dayFromWeekday:(NSDate *)date;
  128. /**
  129. * 日期是否相等
  130. *
  131. * @param anotherDate The another date to compare as NSDate
  132. * @return Return YES if is same day, NO if not
  133. */
  134. - (BOOL)jk_isSameDay:(NSDate *)anotherDate;
  135. /**
  136. * 是否是今天
  137. *
  138. * @return Return if self is today
  139. */
  140. - (BOOL)jk_isToday;
  141. /**
  142. * Add days to self
  143. *
  144. * @param days The number of days to add
  145. * @return Return self by adding the gived days number
  146. */
  147. - (NSDate *)jk_dateByAddingDays:(NSUInteger)days;
  148. /**
  149. * Get the month as a localized string from the given month number
  150. *
  151. * @param month The month to be converted in string
  152. * [1 - January]
  153. * [2 - February]
  154. * [3 - March]
  155. * [4 - April]
  156. * [5 - May]
  157. * [6 - June]
  158. * [7 - July]
  159. * [8 - August]
  160. * [9 - September]
  161. * [10 - October]
  162. * [11 - November]
  163. * [12 - December]
  164. *
  165. * @return Return the given month as a localized string
  166. */
  167. + (NSString *)jk_monthWithMonthNumber:(NSInteger)month;
  168. /**
  169. * 根据日期返回字符串
  170. */
  171. + (NSString *)jk_stringWithDate:(NSDate *)date format:(NSString *)format;
  172. - (NSString *)jk_stringWithFormat:(NSString *)format;
  173. + (NSDate *)jk_dateWithString:(NSString *)string format:(NSString *)format;
  174. /**
  175. * 获取指定月份的天数
  176. */
  177. - (NSUInteger)jk_daysInMonth:(NSUInteger)month;
  178. + (NSUInteger)jk_daysInMonth:(NSDate *)date month:(NSUInteger)month;
  179. /**
  180. * 获取当前月份的天数
  181. */
  182. - (NSUInteger)jk_daysInMonth;
  183. + (NSUInteger)jk_daysInMonth:(NSDate *)date;
  184. /**
  185. * 返回x分钟前/x小时前/昨天/x天前/x个月前/x年前
  186. */
  187. - (NSString *)jk_timeInfo;
  188. + (NSString *)jk_timeInfoWithDate:(NSDate *)date;
  189. + (NSString *)jk_timeInfoWithDateString:(NSString *)dateString;
  190. /**
  191. * 分别获取yyyy-MM-dd/HH:mm:ss/yyyy-MM-dd HH:mm:ss格式的字符串
  192. */
  193. - (NSString *)jk_ymdFormat;
  194. - (NSString *)jk_hmsFormat;
  195. - (NSString *)jk_ymdHmsFormat;
  196. + (NSString *)jk_ymdFormat;
  197. + (NSString *)jk_hmsFormat;
  198. + (NSString *)jk_ymdHmsFormat;
  199. @end