tes*_*dtv 4 iphone cocoa-touch objective-c nsdate
如果我有一个像04-30-2006这样的日期,我怎么能分开并获得月,日和年
还有什么比较岁月的直接方法吗?
Mat*_*uch 17
你必须使用NSDateComponents.像这样:
NSDate *date = [NSDate date];
NSUInteger componentFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponents *components = [[NSCalendar currentCalendar] components:componentFlags fromDate:date];
NSInteger year = [components year];
NSInteger month = [components month];
NSInteger day = [components day];
Run Code Online (Sandbox Code Playgroud)
还有什么比较岁月的直接方法吗?
没有内置.但你可以为它写一个类别.像这样:
@interface NSDate (YearCompare)
- (BOOL)yearIsEqualToDate:(NSDate *)compareDate;
@end
@implementation NSDate (YearCompare)
- (BOOL)yearIsEqualToDate:(NSDate *)compareDate {
NSDateComponents *myComponents = [[NSCalendar currentCalendar] components:NSYearCalendarUnit fromDate:self];
NSDateComponents *otherComponents = [[NSCalendar currentCalendar] components:NSYearCalendarUnit fromDate:compareDate];
if ([myComponents year] == [otherComponents year]) {
return YES;
}
return NO;
}
@end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5472 次 |
| 最近记录: |