NSDate+YYAdd.m 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. //
  2. // NSDate+YYAdd.m
  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 "NSDate+YYAdd.h"
  12. #import "YYKitMacro.h"
  13. #import <time.h>
  14. YYSYNTH_DUMMY_CLASS(NSDate_YYAdd)
  15. @implementation NSDate (YYAdd)
  16. - (NSInteger)year {
  17. return [[[NSCalendar currentCalendar] components:NSCalendarUnitYear fromDate:self] year];
  18. }
  19. - (NSInteger)month {
  20. return [[[NSCalendar currentCalendar] components:NSCalendarUnitMonth fromDate:self] month];
  21. }
  22. - (NSInteger)day {
  23. return [[[NSCalendar currentCalendar] components:NSCalendarUnitDay fromDate:self] day];
  24. }
  25. - (NSInteger)hour {
  26. return [[[NSCalendar currentCalendar] components:NSCalendarUnitHour fromDate:self] hour];
  27. }
  28. - (NSInteger)minute {
  29. return [[[NSCalendar currentCalendar] components:NSCalendarUnitMinute fromDate:self] minute];
  30. }
  31. - (NSInteger)second {
  32. return [[[NSCalendar currentCalendar] components:NSCalendarUnitSecond fromDate:self] second];
  33. }
  34. - (NSInteger)nanosecond {
  35. return [[[NSCalendar currentCalendar] components:NSCalendarUnitSecond fromDate:self] nanosecond];
  36. }
  37. - (NSInteger)weekday {
  38. return [[[NSCalendar currentCalendar] components:NSCalendarUnitWeekday fromDate:self] weekday];
  39. }
  40. - (NSInteger)weekdayOrdinal {
  41. return [[[NSCalendar currentCalendar] components:NSCalendarUnitWeekdayOrdinal fromDate:self] weekdayOrdinal];
  42. }
  43. - (NSInteger)weekOfMonth {
  44. return [[[NSCalendar currentCalendar] components:NSCalendarUnitWeekOfMonth fromDate:self] weekOfMonth];
  45. }
  46. - (NSInteger)weekOfYear {
  47. return [[[NSCalendar currentCalendar] components:NSCalendarUnitWeekOfYear fromDate:self] weekOfYear];
  48. }
  49. - (NSInteger)yearForWeekOfYear {
  50. return [[[NSCalendar currentCalendar] components:NSCalendarUnitYearForWeekOfYear fromDate:self] yearForWeekOfYear];
  51. }
  52. - (NSInteger)quarter {
  53. return [[[NSCalendar currentCalendar] components:NSCalendarUnitQuarter fromDate:self] quarter];
  54. }
  55. - (BOOL)isLeapMonth {
  56. return [[[NSCalendar currentCalendar] components:NSCalendarUnitQuarter fromDate:self] isLeapMonth];
  57. }
  58. - (BOOL)isLeapYear {
  59. NSUInteger year = self.year;
  60. return ((year % 400 == 0) || ((year % 100 != 0) && (year % 4 == 0)));
  61. }
  62. - (BOOL)isToday {
  63. if (fabs(self.timeIntervalSinceNow) >= 60 * 60 * 24) return NO;
  64. return [NSDate new].day == self.day;
  65. }
  66. - (BOOL)isYesterday {
  67. NSDate *added = [self dateByAddingDays:1];
  68. return [added isToday];
  69. }
  70. - (NSDate *)dateByAddingYears:(NSInteger)years {
  71. NSCalendar *calendar = [NSCalendar currentCalendar];
  72. NSDateComponents *components = [[NSDateComponents alloc] init];
  73. [components setYear:years];
  74. return [calendar dateByAddingComponents:components toDate:self options:0];
  75. }
  76. - (NSDate *)dateByAddingMonths:(NSInteger)months {
  77. NSCalendar *calendar = [NSCalendar currentCalendar];
  78. NSDateComponents *components = [[NSDateComponents alloc] init];
  79. [components setMonth:months];
  80. return [calendar dateByAddingComponents:components toDate:self options:0];
  81. }
  82. - (NSDate *)dateByAddingWeeks:(NSInteger)weeks {
  83. NSCalendar *calendar = [NSCalendar currentCalendar];
  84. NSDateComponents *components = [[NSDateComponents alloc] init];
  85. [components setWeekOfYear:weeks];
  86. return [calendar dateByAddingComponents:components toDate:self options:0];
  87. }
  88. - (NSDate *)dateByAddingDays:(NSInteger)days {
  89. NSTimeInterval aTimeInterval = [self timeIntervalSinceReferenceDate] + 86400 * days;
  90. NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval];
  91. return newDate;
  92. }
  93. - (NSDate *)dateByAddingHours:(NSInteger)hours {
  94. NSTimeInterval aTimeInterval = [self timeIntervalSinceReferenceDate] + 3600 * hours;
  95. NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval];
  96. return newDate;
  97. }
  98. - (NSDate *)dateByAddingMinutes:(NSInteger)minutes {
  99. NSTimeInterval aTimeInterval = [self timeIntervalSinceReferenceDate] + 60 * minutes;
  100. NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval];
  101. return newDate;
  102. }
  103. - (NSDate *)dateByAddingSeconds:(NSInteger)seconds {
  104. NSTimeInterval aTimeInterval = [self timeIntervalSinceReferenceDate] + seconds;
  105. NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval];
  106. return newDate;
  107. }
  108. - (NSString *)stringWithFormat:(NSString *)format {
  109. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  110. [formatter setDateFormat:format];
  111. [formatter setLocale:[NSLocale currentLocale]];
  112. return [formatter stringFromDate:self];
  113. }
  114. - (NSString *)stringWithFormat:(NSString *)format timeZone:(NSTimeZone *)timeZone locale:(NSLocale *)locale {
  115. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  116. [formatter setDateFormat:format];
  117. if (timeZone) [formatter setTimeZone:timeZone];
  118. if (locale) [formatter setLocale:locale];
  119. return [formatter stringFromDate:self];
  120. }
  121. - (NSString *)stringWithISOFormat {
  122. static NSDateFormatter *formatter = nil;
  123. static dispatch_once_t onceToken;
  124. dispatch_once(&onceToken, ^{
  125. formatter = [[NSDateFormatter alloc] init];
  126. formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
  127. formatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ssZ";
  128. });
  129. return [formatter stringFromDate:self];
  130. }
  131. + (NSDate *)dateWithString:(NSString *)dateString format:(NSString *)format {
  132. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  133. [formatter setDateFormat:format];
  134. return [formatter dateFromString:dateString];
  135. }
  136. + (NSDate *)dateWithString:(NSString *)dateString format:(NSString *)format timeZone:(NSTimeZone *)timeZone locale:(NSLocale *)locale {
  137. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  138. [formatter setDateFormat:format];
  139. if (timeZone) [formatter setTimeZone:timeZone];
  140. if (locale) [formatter setLocale:locale];
  141. return [formatter dateFromString:dateString];
  142. }
  143. + (NSDate *)dateWithISOFormatString:(NSString *)dateString {
  144. static NSDateFormatter *formatter = nil;
  145. static dispatch_once_t onceToken;
  146. dispatch_once(&onceToken, ^{
  147. formatter = [[NSDateFormatter alloc] init];
  148. formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
  149. formatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ssZ";
  150. });
  151. return [formatter dateFromString:dateString];
  152. }
  153. @end