NSDate+Utilities.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. Erica Sadun, http://ericasadun.com
  3. iPhone Developer's Cookbook 3.x and beyond
  4. BSD License, Use at your own risk
  5. */
  6. #import <Foundation/Foundation.h>
  7. #define D_SECOND 1
  8. #define D_MINUTE 60
  9. #define D_HOUR 3600
  10. #define D_DAY 86400
  11. #define D_WEEK 604800
  12. #define D_YEAR 31556926
  13. @interface NSDate (Utilities)
  14. + (NSCalendar *) currentCalendar; // avoid bottlenecks
  15. + (NSDate *)systemDate;
  16. // Relative dates from the current date
  17. + (NSDate *) dateTomorrow;
  18. + (NSDate *) dateYesterday;
  19. + (NSDate *) dateWithDaysFromNow: (NSInteger) days;
  20. + (NSDate *) dateWithDaysBeforeNow: (NSInteger) days;
  21. + (NSDate *) dateWithHoursFromNow: (NSInteger) dHours;
  22. + (NSDate *) dateWithHoursBeforeNow: (NSInteger) dHours;
  23. + (NSDate *) dateWithMinutesFromNow: (NSInteger) dMinutes;
  24. + (NSDate *) dateWithMinutesBeforeNow: (NSInteger) dMinutes;
  25. // Short string utilities
  26. - (NSString *) stringWithDateStyle: (NSDateFormatterStyle) dateStyle timeStyle: (NSDateFormatterStyle) timeStyle;
  27. - (NSString *) stringWithFormat: (NSString *) format;
  28. @property (nonatomic, readonly) NSString *shortString;
  29. @property (nonatomic, readonly) NSString *shortDateString;
  30. @property (nonatomic, readonly) NSString *shortTimeString;
  31. @property (nonatomic, readonly) NSString *mediumString;
  32. @property (nonatomic, readonly) NSString *mediumDateString;
  33. @property (nonatomic, readonly) NSString *mediumTimeString;
  34. @property (nonatomic, readonly) NSString *longString;
  35. @property (nonatomic, readonly) NSString *longDateString;
  36. @property (nonatomic, readonly) NSString *longTimeString;
  37. // Comparing dates
  38. - (BOOL) isEqualToDateIgnoringTime: (NSDate *) aDate;
  39. - (BOOL) isToday;
  40. - (BOOL) isTomorrow;
  41. - (BOOL) isYesterday;
  42. - (BOOL) isSameWeekAsDate: (NSDate *) aDate;
  43. - (BOOL) isThisWeek;
  44. - (BOOL) isNextWeek;
  45. - (BOOL) isLastWeek;
  46. - (BOOL) isSameMonthAsDate: (NSDate *) aDate;
  47. - (BOOL) isThisMonth;
  48. - (BOOL) isNextMonth;
  49. - (BOOL) isLastMonth;
  50. - (BOOL) isSameYearAsDate: (NSDate *) aDate;
  51. - (BOOL) isThisYear;
  52. - (BOOL) isNextYear;
  53. - (BOOL) isLastYear;
  54. - (BOOL) isEarlierThanDate: (NSDate *) aDate;
  55. - (BOOL) isLaterThanDate: (NSDate *) aDate;
  56. - (BOOL) isInFuture;
  57. - (BOOL) isInPast;
  58. // Date roles
  59. - (BOOL) isTypicallyWorkday;
  60. - (BOOL) isTypicallyWeekend;
  61. // Adjusting dates
  62. - (NSDate *) dateByAddingYears: (NSInteger) dYears;
  63. - (NSDate *) dateBySubtractingYears: (NSInteger) dYears;
  64. - (NSDate *) dateByAddingMonths: (NSInteger) dMonths;
  65. - (NSDate *) dateBySubtractingMonths: (NSInteger) dMonths;
  66. - (NSDate *) dateByAddingDays: (NSInteger) dDays;
  67. - (NSDate *) dateBySubtractingDays: (NSInteger) dDays;
  68. - (NSDate *) dateByAddingHours: (NSInteger) dHours;
  69. - (NSDate *) dateBySubtractingHours: (NSInteger) dHours;
  70. - (NSDate *) dateByAddingMinutes: (NSInteger) dMinutes;
  71. - (NSDate *) dateBySubtractingMinutes: (NSInteger) dMinutes;
  72. // Date extremes
  73. - (NSDate *) dateAtStartOfDay;
  74. - (NSDate *) dateAtEndOfDay;
  75. // Retrieving intervals
  76. - (NSInteger) secondsAfterDate: (NSDate *) aDate;
  77. - (NSInteger) secondsBeforeDate: (NSDate *) aDate;
  78. - (NSInteger) minutesAfterDate: (NSDate *) aDate;
  79. - (NSInteger) minutesBeforeDate: (NSDate *) aDate;
  80. - (NSInteger) hoursAfterDate: (NSDate *) aDate;
  81. - (NSInteger) hoursBeforeDate: (NSDate *) aDate;
  82. - (NSInteger) daysAfterDate: (NSDate *) aDate;
  83. - (NSInteger) daysBeforeDate: (NSDate *) aDate;
  84. - (NSInteger)distanceInDaysToDate:(NSDate *)anotherDate;
  85. // Decomposing dates
  86. @property (readonly) NSInteger nearestHour;
  87. @property (readonly) NSInteger hour;
  88. @property (readonly) NSInteger minute;
  89. @property (readonly) NSInteger seconds;
  90. @property (readonly) NSInteger day;
  91. @property (readonly) NSInteger month;
  92. @property (readonly) NSInteger week;
  93. @property (readonly) NSInteger weekday;
  94. @property (readonly) NSInteger nthWeekday; // e.g. 2nd Tuesday of the month == 2
  95. @property (readonly) NSInteger year;
  96. - (NSDateComponents*)dsDayWithCalendar:(NSCalendar*)calendar;
  97. - (NSDateComponents*)dsMonthWithCalendar:(NSCalendar*)calendar;
  98. - (NSDateComponents*)dsMonth;
  99. @end