相信很多公司都有此需求,百度+google了半天,最终:
NSDateComponents *currentDate = [NSDate dateComponentsWithDateComponents:[NSDate date]];
NSString* string = [NSString stringWithFormat:@"%ld1231160000",currentDate.year];
NSDateFormatter*inputFormatter = [[NSDateFormatter alloc]init];
[inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] ];
[inputFormatter setDateFormat:@"yyyyMMddHHmmss"];
NSDate * inputDate = [inputFormatter dateFromString:string];
NSDate * maxDate = inputDate;
NSDate *minDate = [NSDate dateWithTimeIntervalSince1970:0];
--- 这里是一个分类
+ (NSDateComponents*)dateComponentsWithDateComponents:(NSDate*)endDate{
NSCalendar*calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];//设置成中国阳历
NSDateComponents*comps = [[NSDateComponents alloc]init];
NSIntegerunitFlags =NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSWeekdayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit;
comps = [calendar components:unitFlagsfromDate:endDate];
returncomps;
}