NSDate+YYAdd.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. //
  2. // NSDate+YYAdd.h
  3. // YYKit <https://github.com/ibireme/YYKit>
  4. //
  5. // Created by ibireme on 13/4/11.
  6. // Copyright (c) 2015 ibireme.
  7. //
  8. // This source code is licensed under the MIT-style license found in the
  9. // LICENSE file in the root directory of this source tree.
  10. //
  11. #import <Foundation/Foundation.h>
  12. NS_ASSUME_NONNULL_BEGIN
  13. /**
  14. Provides extensions for `NSDate`.
  15. */
  16. @interface NSDate (YYAdd)
  17. #pragma mark - Component Properties
  18. ///=============================================================================
  19. /// @name Component Properties
  20. ///=============================================================================
  21. @property (nonatomic, readonly) NSInteger year; ///< Year component
  22. @property (nonatomic, readonly) NSInteger month; ///< Month component (1~12)
  23. @property (nonatomic, readonly) NSInteger day; ///< Day component (1~31)
  24. @property (nonatomic, readonly) NSInteger hour; ///< Hour component (0~23)
  25. @property (nonatomic, readonly) NSInteger minute; ///< Minute component (0~59)
  26. @property (nonatomic, readonly) NSInteger second; ///< Second component (0~59)
  27. @property (nonatomic, readonly) NSInteger nanosecond; ///< Nanosecond component
  28. @property (nonatomic, readonly) NSInteger weekday; ///< Weekday component (1~7, first day is based on user setting)
  29. @property (nonatomic, readonly) NSInteger weekdayOrdinal; ///< WeekdayOrdinal component
  30. @property (nonatomic, readonly) NSInteger weekOfMonth; ///< WeekOfMonth component (1~5)
  31. @property (nonatomic, readonly) NSInteger weekOfYear; ///< WeekOfYear component (1~53)
  32. @property (nonatomic, readonly) NSInteger yearForWeekOfYear; ///< YearForWeekOfYear component
  33. @property (nonatomic, readonly) NSInteger quarter; ///< Quarter component
  34. @property (nonatomic, readonly) BOOL isLeapMonth; ///< whether the month is leap month
  35. @property (nonatomic, readonly) BOOL isLeapYear; ///< whether the year is leap year
  36. @property (nonatomic, readonly) BOOL isToday; ///< whether date is today (based on current locale)
  37. @property (nonatomic, readonly) BOOL isYesterday; ///< whether date is yesterday (based on current locale)
  38. #pragma mark - Date modify
  39. ///=============================================================================
  40. /// @name Date modify
  41. ///=============================================================================
  42. /**
  43. Returns a date representing the receiver date shifted later by the provided number of years.
  44. @param years Number of years to add.
  45. @return Date modified by the number of desired years.
  46. */
  47. - (nullable NSDate *)dateByAddingYears:(NSInteger)years;
  48. /**
  49. Returns a date representing the receiver date shifted later by the provided number of months.
  50. @param months Number of months to add.
  51. @return Date modified by the number of desired months.
  52. */
  53. - (nullable NSDate *)dateByAddingMonths:(NSInteger)months;
  54. /**
  55. Returns a date representing the receiver date shifted later by the provided number of weeks.
  56. @param weeks Number of weeks to add.
  57. @return Date modified by the number of desired weeks.
  58. */
  59. - (nullable NSDate *)dateByAddingWeeks:(NSInteger)weeks;
  60. /**
  61. Returns a date representing the receiver date shifted later by the provided number of days.
  62. @param days Number of days to add.
  63. @return Date modified by the number of desired days.
  64. */
  65. - (nullable NSDate *)dateByAddingDays:(NSInteger)days;
  66. /**
  67. Returns a date representing the receiver date shifted later by the provided number of hours.
  68. @param hours Number of hours to add.
  69. @return Date modified by the number of desired hours.
  70. */
  71. - (nullable NSDate *)dateByAddingHours:(NSInteger)hours;
  72. /**
  73. Returns a date representing the receiver date shifted later by the provided number of minutes.
  74. @param minutes Number of minutes to add.
  75. @return Date modified by the number of desired minutes.
  76. */
  77. - (nullable NSDate *)dateByAddingMinutes:(NSInteger)minutes;
  78. /**
  79. Returns a date representing the receiver date shifted later by the provided number of seconds.
  80. @param seconds Number of seconds to add.
  81. @return Date modified by the number of desired seconds.
  82. */
  83. - (nullable NSDate *)dateByAddingSeconds:(NSInteger)seconds;
  84. #pragma mark - Date Format
  85. ///=============================================================================
  86. /// @name Date Format
  87. ///=============================================================================
  88. /**
  89. Returns a formatted string representing this date.
  90. see http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns
  91. for format description.
  92. @param format String representing the desired date format.
  93. e.g. @"yyyy-MM-dd HH:mm:ss"
  94. @return NSString representing the formatted date string.
  95. */
  96. - (nullable NSString *)stringWithFormat:(NSString *)format;
  97. /**
  98. Returns a formatted string representing this date.
  99. see http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns
  100. for format description.
  101. @param format String representing the desired date format.
  102. e.g. @"yyyy-MM-dd HH:mm:ss"
  103. @param timeZone Desired time zone.
  104. @param locale Desired locale.
  105. @return NSString representing the formatted date string.
  106. */
  107. - (nullable NSString *)stringWithFormat:(NSString *)format
  108. timeZone:(nullable NSTimeZone *)timeZone
  109. locale:(nullable NSLocale *)locale;
  110. /**
  111. Returns a string representing this date in ISO8601 format.
  112. e.g. "2010-07-09T16:13:30+12:00"
  113. @return NSString representing the formatted date string in ISO8601.
  114. */
  115. - (nullable NSString *)stringWithISOFormat;
  116. /**
  117. Returns a date parsed from given string interpreted using the format.
  118. @param dateString The string to parse.
  119. @param format The string's date format.
  120. @return A date representation of string interpreted using the format.
  121. If can not parse the string, returns nil.
  122. */
  123. + (nullable NSDate *)dateWithString:(NSString *)dateString format:(NSString *)format;
  124. /**
  125. Returns a date parsed from given string interpreted using the format.
  126. @param dateString The string to parse.
  127. @param format The string's date format.
  128. @param timeZone The time zone, can be nil.
  129. @param locale The locale, can be nil.
  130. @return A date representation of string interpreted using the format.
  131. If can not parse the string, returns nil.
  132. */
  133. + (nullable NSDate *)dateWithString:(NSString *)dateString
  134. format:(NSString *)format
  135. timeZone:(nullable NSTimeZone *)timeZone
  136. locale:(nullable NSLocale *)locale;
  137. /**
  138. Returns a date parsed from given string interpreted using the ISO8601 format.
  139. @param dateString The date string in ISO8601 format. e.g. "2010-07-09T16:13:30+12:00"
  140. @return A date representation of string interpreted using the format.
  141. If can not parse the string, returns nil.
  142. */
  143. + (nullable NSDate *)dateWithISOFormatString:(NSString *)dateString;
  144. @end
  145. NS_ASSUME_NONNULL_END