//返回0
当天,1昨天,2前天,以此类推
//之前那样用时间差来整除一天的秒数,计算出来的天数有误差
+ (int)getDaysFromAnotherDay:(longlong)date {//date是时间戳
// return [self getDaysFrom1970:date] - [self getDaysFrom1970:[[NSDate date] timeIntervalSince1970]];
NSDateComponents *comps = [[NSDateComponentsalloc]
init];
[compssetDay:[NSDateUtilsgetDay:date]];
[compssetMonth:[NSDateUtilsgetMonth:date]];
[compssetYear:[NSDateUtilsgetYear:date]];
NSCalendar *gregorian = [[NSCalendaralloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *endDate = [[NSDatealloc]
init];
NSDate *startDate = [gregorian
dateFromComponents:comps];
[compsrelease];
unsignedint unitFlags =NSYearCalendarUnit |
NSMonthCalendarUnit |NSDayCalendarUnit;
NSDateComponents *comps2 = [gregorian
components:unitFlags
fromDate:startDatetoDate:endDate
options:0];
int days = [comps2
day];
return days;
}
+ (int)getYear:(longlong)date {
return [[selfgetNSDateComponents:date]
year];
}
+ (int)getMonth:(longlong)date {
return [[selfgetNSDateComponents:date]
month];
}
+ (int)getDay:(longlong)date {
return [[selfgetNSDateComponents:date]
day];
}
+ (NSDateComponents *)getNSDateComponents:(long
long)date {
NSInteger unitFlags =
NSYearCalendarUnit | NSMonthCalendarUnit |
NSDayCalendarUnit | NSWeekdayCalendarUnit |
NSHourCalendarUnit | NSMinuteCalendarUnit |
NSSecondCalendarUnit;
NSDateComponents *comps = [[self
shareNSCalendar]
components:unitFlags fromDate:[NSDate
dateWithTimeIntervalSince1970:date]];
return comps;
}