NSDate+Extension.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. //
  2. // NSDate+Extension.h
  3. // iOS-Categories (https://github.com/shaojiankui/iOS-Categories)
  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 (Extension)
  10. /**
  11. * 获取日、月、年、小时、分钟、秒
  12. */
  13. - (NSUInteger)day;
  14. - (NSUInteger)month;
  15. - (NSUInteger)year;
  16. - (NSUInteger)hour;
  17. - (NSUInteger)minute;
  18. - (NSUInteger)second;
  19. + (NSUInteger)day:(NSDate *)date;
  20. + (NSUInteger)month:(NSDate *)date;
  21. + (NSUInteger)year:(NSDate *)date;
  22. + (NSUInteger)hour:(NSDate *)date;
  23. + (NSUInteger)minute:(NSDate *)date;
  24. + (NSUInteger)second:(NSDate *)date;
  25. /**
  26. * 获取一年中的总天数
  27. */
  28. - (NSUInteger)daysInYear;
  29. + (NSUInteger)daysInYear:(NSDate *)date;
  30. /**
  31. * 判断是否是润年
  32. * @return YES表示润年,NO表示平年
  33. */
  34. - (BOOL)isLeapYear;
  35. + (BOOL)isLeapYear:(NSDate *)date;
  36. /**
  37. * 获取该日期是该年的第几周
  38. */
  39. - (NSUInteger)weekOfYear;
  40. + (NSUInteger)weekOfYear:(NSDate *)date;
  41. /**
  42. * 获取格式化为YYYY-MM-dd格式的日期字符串
  43. */
  44. - (NSString *)formatYMD;
  45. + (NSString *)formatYMD:(NSDate *)date;
  46. /**
  47. * 返回当前月一共有几周(可能为4,5,6)
  48. */
  49. - (NSUInteger)weeksOfMonth;
  50. + (NSUInteger)weeksOfMonth:(NSDate *)date;
  51. /**
  52. * 获取该月的第一天的日期
  53. */
  54. - (NSDate *)begindayOfMonth;
  55. + (NSDate *)begindayOfMonth:(NSDate *)date;
  56. /**
  57. * 获取该月的最后一天的日期
  58. */
  59. - (NSDate *)lastdayOfMonth;
  60. + (NSDate *)lastdayOfMonth:(NSDate *)date;
  61. /**
  62. * 返回day天后的日期(若day为负数,则为|day|天前的日期)
  63. */
  64. - (NSDate *)dateAfterDay:(NSUInteger)day;
  65. + (NSDate *)dateAfterDate:(NSDate *)date day:(NSInteger)day;
  66. /**
  67. * 返回day天后的日期(若day为负数,则为|day|天前的日期)
  68. */
  69. - (NSDate *)dateAfterMonth:(NSUInteger)month;
  70. + (NSDate *)dateAfterDate:(NSDate *)date month:(NSInteger)month;
  71. /**
  72. * 返回numYears年后的日期
  73. */
  74. - (NSDate *)offsetYears:(int)numYears;
  75. + (NSDate *)offsetYears:(int)numYears fromDate:(NSDate *)fromDate;
  76. /**
  77. * 返回numMonths月后的日期
  78. */
  79. - (NSDate *)offsetMonths:(int)numMonths;
  80. + (NSDate *)offsetMonths:(int)numMonths fromDate:(NSDate *)fromDate;
  81. /**
  82. * 返回numDays天后的日期
  83. */
  84. - (NSDate *)offsetDays:(int)numDays;
  85. + (NSDate *)offsetDays:(int)numDays fromDate:(NSDate *)fromDate;
  86. /**
  87. * 返回numHours小时后的日期
  88. */
  89. - (NSDate *)offsetHours:(int)hours;
  90. + (NSDate *)offsetHours:(int)numHours fromDate:(NSDate *)fromDate;
  91. /**
  92. * 距离该日期前几天
  93. */
  94. - (NSUInteger)daysAgo;
  95. + (NSUInteger)daysAgo:(NSDate *)date;
  96. /**
  97. * 获取星期几
  98. *
  99. * @return Return weekday number
  100. * [1 - Sunday]
  101. * [2 - Monday]
  102. * [3 - Tuerday]
  103. * [4 - Wednesday]
  104. * [5 - Thursday]
  105. * [6 - Friday]
  106. * [7 - Saturday]
  107. */
  108. - (NSInteger)weekday;
  109. + (NSInteger)weekday:(NSDate *)date;
  110. /**
  111. * 获取星期几(名称)
  112. *
  113. * @return Return weekday as a localized string
  114. * [1 - Sunday]
  115. * [2 - Monday]
  116. * [3 - Tuerday]
  117. * [4 - Wednesday]
  118. * [5 - Thursday]
  119. * [6 - Friday]
  120. * [7 - Saturday]
  121. */
  122. - (NSString *)dayFromWeekday;
  123. + (NSString *)dayFromWeekday:(NSDate *)date;
  124. /**
  125. * 日期是否相等
  126. *
  127. * @param anotherDate The another date to compare as NSDate
  128. * @return Return YES if is same day, NO if not
  129. */
  130. - (BOOL)isSameDay:(NSDate *)anotherDate;
  131. /**
  132. * 是否是今天
  133. *
  134. * @return Return if self is today
  135. */
  136. - (BOOL)isToday;
  137. /**
  138. * Add days to self
  139. *
  140. * @param days The number of days to add
  141. * @return Return self by adding the gived days number
  142. */
  143. - (NSDate *)dateByAddingDays:(NSUInteger)days;
  144. /**
  145. * Get the month as a localized string from the given month number
  146. *
  147. * @param month The month to be converted in string
  148. * [1 - January]
  149. * [2 - February]
  150. * [3 - March]
  151. * [4 - April]
  152. * [5 - May]
  153. * [6 - June]
  154. * [7 - July]
  155. * [8 - August]
  156. * [9 - September]
  157. * [10 - October]
  158. * [11 - November]
  159. * [12 - December]
  160. *
  161. * @return Return the given month as a localized string
  162. */
  163. + (NSString *)monthWithMonthNumber:(NSInteger)month;
  164. /**
  165. * 根据日期返回字符串
  166. */
  167. + (NSString *)stringWithDate:(NSDate *)date format:(NSString *)format;
  168. - (NSString *)stringWithFormat:(NSString *)format;
  169. + (NSDate *)dateWithString:(NSString *)string format:(NSString *)format;
  170. /**
  171. * 获取指定月份的天数
  172. */
  173. - (NSUInteger)daysInMonth:(NSUInteger)month;
  174. + (NSUInteger)daysInMonth:(NSDate *)date month:(NSUInteger)month;
  175. /**
  176. * 获取当前月份的天数
  177. */
  178. - (NSUInteger)daysInMonth;
  179. + (NSUInteger)daysInMonth:(NSDate *)date;
  180. /**
  181. * 返回x分钟前/x小时前/昨天/x天前/x个月前/x年前
  182. */
  183. - (NSString *)timeInfo;
  184. + (NSString *)timeInfoWithDate:(NSDate *)date;
  185. + (NSString *)timeInfoWithDateString:(NSString *)dateString;
  186. /**
  187. * 分别获取yyyy-MM-dd/HH:mm:ss/yyyy-MM-dd HH:mm:ss格式的字符串
  188. */
  189. - (NSString *)ymdFormat;
  190. - (NSString *)hmsFormat;
  191. - (NSString *)ymdHmsFormat;
  192. + (NSString *)ymdFormat;
  193. + (NSString *)hmsFormat;
  194. + (NSString *)ymdHmsFormat;
  195. @end