How can I create an NSDate
from the day, month and year? There don\'t seem to be any methods to do this and they have removed the class method dateWithString<
You could write a category for this. I did that, this is how the code looks:
// NSDateCategory.h
#import
@interface NSDate (MBDateCat)
+ (NSDate *)dateWithYear:(NSInteger)year month:(NSInteger)month day:(NSInteger)day;
@end
// NSDateCategory.m
#import "NSDateCategory.h"
@implementation NSDate (MBDateCat)
+ (NSDate *)dateWithYear:(NSInteger)year month:(NSInteger)month day:(NSInteger)day {
NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
[components setYear:year];
[components setMonth:month];
[components setDay:day];
return [calendar dateFromComponents:components];
}
@end
Use it like this: NSDate *aDate = [NSDate dateWithYear:2010 month:5 day:12];