CocoaPods trunk is moving to be read-only. Read more on the blog, there are 18 months to go.

MCDateExtensions 1.0.0

MCDateExtensions 1.0.0

TestsTested
LangLanguage Obj-CObjective C
License BSD 3.0
ReleasedLast Release Dec 2014

Maintained by Mirego.



  • By
  • Mirego, Inc.

MCDateExtensions - Having fun with NSDate.

#import "NSDate+MCExtensions.h"

- (void)funWithMCDateExtensions
{
  NSDate* today = [NSDate date];

  // Today? Tomorrow? Yesterday?
  [today isToday]; // returns YES
  [today isYesterday]; // returns NO
  [today isTomorrow]; // returns NO

  // Compare based on day
  [today isSameDayAsDate:[NSDate date]]; // Most likely returns YES

  // Get all the details of a date
  NSDateComponents* todayBrokenDown = [today components];
  // ...with specific units
  NSDateComponents* specificUnits = [today componentsWithUnits:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit)];

  // Some fun Maths!
  NSDate* sameDayNextYear = [today dateByAddingYears:1];
  NSDate* sameDayNextQuarter = [today dateByAddingQuarters:1];
  NSDate* sameDayInTwoMonth = [today dateByAddingMonths:2];
  NSDate* sameDayNextWeek = [today dateByAddingWeeks:1];
  NSDate* yesterday = [today dateByAddingDays:-1];
  NSDate* tomorrow = [today dateByAddingHours:24];
  NSDate* nextHour = [today dateByAddingMinutes:60];
  NSDate* inTwoMinutes = [today dateByAddingSeconds:120];
  NSDate* inAFewSeconds = [today dateByAddingCalendarUnit:(NSSecondCalendarUnit) value:30];

  // Differences
  NSInteger days = [NSDate daysBetweenDate:today andDate:tomorrow]; // returns 1
  NSInteger weeks = [NSDate daysBetweenDate:today andDate:sameDayNextWeek]; // returns 1
  NSInteger months = [NSDate monthsBetweenDate:today andDate:sameDayInTwoMonth]; // returns 2
  NSInteger years = [NSDate monthsBetweenDate:today andDate:sameDayNextYear]; // returns 1
}

Adding to your project

If you're using CocoaPods, there's nothing simpler. Add the following to your Podfile and run pod install

pod 'MCDateExtensions', :git => 'https://github.com/mirego/MCDateExtensions.git'

Don't forget to #import "NSDate+MCExtensions.h" where it's needed.

License

MCDateExtensions is © 2013 Mirego and may be freely distributed under the New BSD license. See the LICENSE.md file.

About Mirego

Mirego is a team of passionate people who believe that work is a place where you can innovate and have fun. We proudly built mobile applications for iPhone, iPad, Android, Blackberry, Windows Phone and Windows 8. Learn more about our team at life.mirego.com.