我正在寻找类似的东西:
NSDate *date = [[NSDate alloc] initWithYear:1984 month:10 Day:8];
有没有办法做这样的事情?
谢谢!
答案 0 :(得分:20)
我为这个任务写了一个类别。 NSDate缺少许多有用的方法。
@interface NSDate (missingFunctions)
+ (NSDate *)dateWithYear:(NSInteger)year month:(NSInteger)month day:(NSInteger)day;
@end
@implementation NSDate (missingFunctions)
+ (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