• Stars
    star
    343
  • Rank 123,371 (Top 3 %)
  • Language
    Objective-C
  • License
    BSD 2-Clause "Sim...
  • Created about 12 years ago
  • Updated almost 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A thread safe date calculation library with all the date functions you'll ever need.

MTDates

Build Status Version Platform

MTDates is a category on NSDate that makes common date calculations super easy.

Installation

In your Podfile, add this line:

pod "MTDates"

pod? => https://github.com/CocoaPods/CocoaPods/ NOTE: You may need to add -all_load to "Other Linker Flags" in your targets build settings if the pods library only contains categories.

Example Usage

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"MM/dd/yyyy hh:mma";
NSDate *date = [formatter dateFromString:@"07/11/1986 11:29am"];

[date mt_year];                                                     // => 1986

[date mt_weekdayOfWeek];                                            // => 6

[date mt_minuteOfHour];                                             // => 29

[[date mt_startOfPreviousYear] mt_year];                            // => 1985

[[date mt_startOfPreviousYear] mt_stringFromDateWithISODateTime];   // => @"1985-01-01T00:00:00Z"

[[date mt_startOfPreviousMonth] mt_stringFromDatesFullMonth];       // => @"June"

Creating Dates

[NSDate mt_dateFromYear:1986 week:28 weekday:6 hour:11 minute:29 second:0];

Dates From Other Dates

[date mt_dateByAddingYears:0 months:0 weeks:2 days:0 hours:1 minutes:24 seconds:0];

[date mt_dateYearsAfter:8];

[date mt_oneYearNext];

[date mt_dateMonthsBefore:4];

Comparing Dates

[date mt_isAfter:date2];

[date2 mt_isBefore:date];

[date3 mt_isOnOrBefore:date];

[date2 mt_isWithinSameWeek:date];

[date4 mt_isWithinSameDay:date];

Other Coolness

[date mt_weekdayStartOfCurrentMonth];
[date mt_daysInCurrentMonth];
[date mt_daysInPreviousMonth];
[date mt_daysInNextMonth];

International

[NSDate mt_setFirstDayOfWeek:1];
[NSDate mt_setFirstDayOfWeek:4];

[NSDate mt_setWeekNumberingSystem:MTDateWeekNumberingSystemISO];
[NSDate mt_setWeekNumberingSystem:MTDateWeekNumberingSystemUS];

All methods

+ (void)mt_setLocale:(NSLocale *)locale;
+ (void)mt_setTimeZone:(NSTimeZone *)timeZone;
+ (void)mt_setFirstDayOfWeek:(NSUInteger)firstDay; // Sunday: 1, Saturday: 7
+ (void)mt_setWeekNumberingSystem:(MTDateWeekNumberingSystem)system;

+ (NSDate *)mt_dateFromISOString:(NSString *)ISOString;
+ (NSDate *)mt_dateFromString:(NSString *)string usingFormat:(NSString *)format;
+ (NSDate *)mt_dateFromYear:(NSUInteger)year month:(NSUInteger)month day:(NSUInteger)day;
+ (NSDate *)mt_dateFromYear:(NSUInteger)year month:(NSUInteger)month day:(NSUInteger)day hour:(NSUInteger)hour minute:(NSUInteger)minute;
+ (NSDate *)mt_dateFromYear:(NSUInteger)year month:(NSUInteger)month day:(NSUInteger)day hour:(NSUInteger)hour minute:(NSUInteger)minute second:(NSUInteger)second;
+ (NSDate *)mt_dateFromYear:(NSUInteger)year week:(NSUInteger)week weekday:(NSUInteger)weekday;
+ (NSDate *)mt_dateFromYear:(NSUInteger)year week:(NSUInteger)week weekday:(NSUInteger)weekday hour:(NSUInteger)hour minute:(NSUInteger)minute;
+ (NSDate *)mt_dateFromYear:(NSUInteger)year week:(NSUInteger)week weekday:(NSUInteger)weekday hour:(NSUInteger)hour minute:(NSUInteger)minute second:(NSUInteger)second;
- (NSDate *)mt_dateByAddingYears:(NSInteger)years months:(NSInteger)months weeks:(NSInteger)weeks days:(NSInteger)days hours:(NSInteger)hours minutes:(NSInteger)minutes seconds:(NSInteger)seconds;
+ (NSDate *)mt_dateFromComponents:(NSDateComponents *)components;

+ (NSArray *)mt_shortWeekdaySymbols;
+ (NSArray *)mt_weekdaySymbols;
+ (NSArray *)mt_veryShortWeekdaySymbols;
+ (NSArray *)mt_shortMonthlySymbols;
+ (NSArray *)mt_monthlySymbols;
+ (NSArray *)mt_veryShortMonthlySymbols;

- (NSUInteger)mt_year;
- (NSUInteger)mt_weekOfYear;
- (NSUInteger)mt_weekdayOfWeek;
- (NSUInteger)mt_monthOfYear;
- (NSUInteger)mt_dayOfMonth;
- (NSUInteger)mt_hourOfDay;
- (NSUInteger)mt_minuteOfHour;
- (NSUInteger)mt_secondOfMinute;
- (NSTimeInterval)mt_secondsIntoDay;
- (NSDateComponents *)mt_components;

- (NSDate *)mt_startOfPreviousYear;
- (NSDate *)mt_startOfCurrentYear;
- (NSDate *)mt_startOfNextYear;
- (NSDate *)mt_endOfPreviousYear;
- (NSDate *)mt_endOfCurrentYear;
- (NSDate *)mt_endOfNextYear;
- (NSDate *)mt_oneYearPrevious;
- (NSDate *)mt_oneYearNext;
- (NSDate *)mt_dateYearsBefore:(NSUInteger)years;
- (NSDate *)mt_dateYearsAfter:(NSUInteger)years;
- (NSInteger)mt_yearsSinceDate:(NSDate *)date;
- (NSInteger)mt_yearsUntilDate:(NSDate *)date;

- (NSDate *)mt_startOfPreviousMonth;
- (NSDate *)mt_startOfCurrentMonth;
- (NSDate *)mt_startOfNextMonth;
- (NSDate *)mt_endOfPreviousMonth;
- (NSDate *)mt_endOfCurrentMonth;
- (NSDate *)mt_endOfNextMonth;
- (NSDate *)mt_oneMonthPrevious;
- (NSDate *)mt_oneMonthNext;
- (NSDate *)mt_dateMonthsBefore:(NSUInteger)months;
- (NSDate *)mt_dateMonthsAfter:(NSUInteger)months;
- (NSInteger)mt_monthsSinceDate:(NSDate *)date;
- (NSInteger)mt_monthsUntilDate:(NSDate *)date;

- (NSDate *)mt_startOfPreviousWeek;
- (NSDate *)mt_startOfCurrentWeek;
- (NSDate *)mt_startOfNextWeek;
- (NSDate *)mt_endOfPreviousWeek;
- (NSDate *)mt_endOfCurrentWeek;
- (NSDate *)mt_endOfNextWeek;
- (NSDate *)mt_oneWeekPrevious;
- (NSDate *)mt_oneWeekNext;
- (NSDate *)mt_dateWeeksBefore:(NSUInteger)weeks;
- (NSDate *)mt_dateWeeksAfter:(NSUInteger)weeks;
- (NSInteger)mt_weeksSinceDate:(NSDate *)date;
- (NSInteger)mt_weeksUntilDate:(NSDate *)date;

- (NSDate *)mt_startOfPreviousDay;
- (NSDate *)mt_startOfCurrentDay;
- (NSDate *)mt_startOfNextDay;
- (NSDate *)mt_endOfPreviousDay;
- (NSDate *)mt_endOfCurrentDay;
- (NSDate *)mt_endOfNextDay;
- (NSDate *)mt_oneDayPrevious;
- (NSDate *)mt_oneDayNext;
- (NSDate *)mt_dateDaysBefore:(NSUInteger)days;
- (NSDate *)mt_dateDaysAfter:(NSUInteger)days;
- (NSInteger)mt_daysSinceDate:(NSDate *)date;
- (NSInteger)mt_daysUntilDate:(NSDate *)date;

- (NSDate *)mt_startOfPreviousHour;
- (NSDate *)mt_startOfCurrentHour;
- (NSDate *)mt_startOfNextHour;
- (NSDate *)mt_endOfPreviousHour;
- (NSDate *)mt_endOfCurrentHour;
- (NSDate *)mt_endOfNextHour;
- (NSDate *)mt_oneHourPrevious;
- (NSDate *)mt_oneHourNext;
- (NSDate *)mt_dateHoursBefore:(NSUInteger)hours;
- (NSDate *)mt_dateHoursAfter:(NSUInteger)hours;
- (NSInteger)mt_hoursSinceDate:(NSDate *)date;
- (NSInteger)mt_hoursUntilDate:(NSDate *)date;

- (BOOL)mt_isAfter:(NSDate *)date;
- (BOOL)mt_isBefore:(NSDate *)date;
- (BOOL)mt_isOnOrAfter:(NSDate *)date;
- (BOOL)mt_isOnOrBefore:(NSDate *)date;
- (BOOL)mt_isWithinSameYear:(NSDate *)date;
- (BOOL)mt_isWithinSameMonth:(NSDate *)date;
- (BOOL)mt_isWithinSameWeek:(NSDate *)date;
- (BOOL)mt_isWithinSameDay:(NSDate *)date;
- (BOOL)mt_isWithinSameHour:(NSDate *)date;
- (BOOL)mt_isBetweenDate:(NSDate *)date1 andDate:(NSDate *)date2;

- (NSString *)mt_stringFromDateWithHourAndMinuteFormat:(MTDateHourFormat)format;
- (NSString *)mt_stringFromDateWithShortMonth;
- (NSString *)mt_stringFromDateWithFullMonth;
- (NSString *)mt_stringFromDateWithAMPMSymbol;
- (NSString *)mt_stringFromDateWithShortWeekdayTitle;
- (NSString *)mt_stringFromDateWithFullWeekdayTitle;
- (NSString *)mt_stringFromDateWithFormat:(NSString *)format localized:(BOOL)localized // http://unicode.org/reports/tr35/tr35-10.html#Date_Format_Patterns
- (NSString *)mt_stringFromDateWithISODateTime;
- (NSString *)mt_stringFromDateWithGreatestComponentsForSecondsPassed:(NSTimeInterval)interval;

+ (NSArray *)mt_datesCollectionFromDate:(NSDate *)startDate untilDate:(NSDate *)endDate;
- (NSArray *)mt_hoursInCurrentDayAsDatesCollection;
- (BOOL)mt_isInAM;
- (BOOL)mt_isStartOfAnHour;
- (NSUInteger)mt_weekdayStartOfCurrentMonth;
- (NSUInteger)mt_daysInCurrentMonth;
- (NSUInteger)mt_daysInPreviousMonth;
- (NSUInteger)mt_daysInNextMonth;
- (NSDate *)mt_inTimeZone:(NSTimeZone *)timezone;

NSDateComponents Additions

NSDateComponents *comps = [NSDateComponents componentsFromString:@"10 October 2009"];
[comps mt_stringValue]   // => @"10 October 2009"

NSDateComponents *comps = [NSDateComponents componentsFromString:@"October 2009"];
[comps mt_stringValue]   // => @"October 2009"

NSDateComponents *comps = [NSDateComponents componentsFromString:@"2009"];
[comps mt_stringValue]   // => @"2009"

NSDateComponents *comps = [NSDateComponents componentsFromString:@"10 2009"];
[comps mt_stringValue]   // => @"October 2009"

NSDateComponents *comps = [NSDateComponents componentsFromString:@"10 July"];
[comps mt_stringValue]   // => @"10 July"

NSDateComponents *comps = [NSDateComponents componentsFromString:@"10"];
[comps mt_stringValue]   // => @"October"

Contributors

More Repositories

1

MTMigration

Manages blocks of code that only need to run once on version updates in iOS apps.
Objective-C
913
star
2

MTAnimation

Animate UIView with 25+ timing functions. (Bounce, elastic, exponential, etc.)
Objective-C
505
star
3

MTGeometry

An extension to Core Graphics Geometry. Intersections, scaling, etc.
Objective-C
212
star
4

MTPDF

Objective-C PDF objects. Doing my part to help us stay out of the headache that is Core Foundation.
Objective-C
205
star
5

MTStringAttributes

Easier way to create an attributes dictionary for NSAttributedString
Objective-C
154
star
6

MTControl

Attach block handlers to UIControl objects jQuery style
Objective-C
114
star
7

MTQueue

Add blocks to queues in a super terse and readable way!
Objective-C
61
star
8

MYSCategoryProperties

Quickly add properties to categories using runtime associations.
Objective-C
60
star
9

MTPencil

Draw animated lines (like an invisible pencil) with Core Graphics. (Prototype, will get much better).
Objective-C
47
star
10

MTFittedScrollView

A UIScrollView subclass that resizes itself to fit around its content.
Objective-C
39
star
11

MYSForms

Easy forms for iOS
Objective-C
33
star
12

MYSRuntime

Obj-C Library that makes runtime self-inspection and class modification dead easy.
Objective-C
33
star
13

MTColorDistance

A category on UIColor. Pass in an array of colors and it will return the closest color to the receiver.
Objective-C
24
star
14

MTPocket

Cocoa Web Requests
Objective-C
23
star
15

EKEventToiCal

Returns a string in iCal format for an EKEvent object
Objective-C
22
star
16

MTBlockAlertView

An iOS Alert View that uses block-based delegation instead of protocols.
Objective-C
22
star
17

FatSecretKit

iOS client library for the FatSecret API
Objective-C
21
star
18

MYSSharedSettings

Access local settings and settings shared via iCloud with properties.
Objective-C
20
star
19

MTDateComponentsPicker

A picker for choosing date components. Allows some components to be undefined.
Objective-C
15
star
20

FakeContacts

An XCode project you can build onto the simulator or device to generate any number or random contacts for app testing
Objective-C
13
star
21

MYSCoreText

An Objective-C wrapper around Apple's Core Text framework.
Objective-C
11
star
22

MYSDynamicAlertView

An alert view that uses UIKit Dynamics to present a fun/interactive way to respond to alerts.
Objective-C
9
star
23

flunk

A gem for testing Ruby on Rails web APIs by simulating a client.
Ruby
6
star
24

MYSShared

A submodule that includes objc code that will more than likely be used in any objc project we do.
Objective-C
3
star
25

MTTimer

An Objective-C timer that restricts firing to a time range. If it's called BEFORE min, it waits for min. If it's called AFTER max, it's called at max.
Objective-C
3
star
26

MYSGravityActionSheet

An action sheet that uses UIKit Dynamics to present buttons in a playful, interesting way.
Objective-C
2
star
27

MYSTableView

Objective-C
1
star
28

Pocket

A curl gem that doesn't suck
Ruby
1
star