NSDate+Utilities.h 3.7 KB

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