- (BOOL)isEqualToDate:(NSDate *)otherDate;
與otherDate比較,相同返回YES
- (NSDate *)earlierDate:(NSDate *)anotherDate;
與anotherDate比較,返回較早的那個日期
- (NSDate *)laterDate:(NSDate *)anotherDate;
與anotherDate比較,返回較晚的那個日期
- (NSComparisonResult)compare:(NSDate *)other;
該方法用于排序時調(diào)用:
. 當(dāng)實例保存的日期值與anotherDate相同時返回NSOrderedSame
. 當(dāng)實例保存的日期值晚于anotherDate時返回NSOrderedDescending
. 當(dāng)實例保存的日期值早于anotherDate時返回NSOrderedAscending // 獲得本地時間指定時區(qū) NSDate *dates = [NSDate date]; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/beijing"]; [formatter setTimeZone:timeZone]; NSString *loctime = [formatter stringFromDate:dates];
(NSDate *) stringToDate:string { NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSDate *date = [dateFormatter dateFromString:string]; [dateFormatter release]; return date; }
-(NSInteger)daysWithinEraFromDate:(NSDate *) startDate toDate:(NSDate *) endDate { NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; unsigned int unitFlags = NSDayCalendarUnit; NSDateComponents *comps = [gregorian components:unitFlags fromDate:startDate toDate:endDate options:0]; int days = [comps day]; return days; }
|