NSString *bill_timeStr=@"2016-08-11 12:12:47";
NSDate *date1=[self datejishuangYear:0 Month:0 Day:1 withData:bill_timeStr];//计算两个之后的时间
/**
* 时间算法
*
* @author Aron
* @date 2016-01-06
*
* @param year 计算年=》加1年:1;减1年:-1
* @param month 计算月=》加1月:1;减1月:-1
* @param day 计算天=》加一周:7;减一周:-7
* @param date 要计算的时间
*
* @return 计算好的时间
*/
-(NSDate *)datejishuangYear:(int)year Month:(int)month Day:(int)day withData:(NSString *)datestring {
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
//NSCalendarIdentifierGregorian:iOS8之前用NSGregorianCalendar
NSDateComponents *comps = nil;
NSString *string=[datestring substringToIndex:10];
NSDate*date =[[NSDate alloc]init];
NSDateFormatter*df = [[NSDateFormatter alloc]init];//格式化
[df setDateFormat:@"yyyy-MM-dd"];
[df setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"] ];
NSTimeZone *timeZone=[NSTimeZone timeZoneWithName:@"UTC"];
[df setTimeZone:timeZone];
date =[df dateFromString:string];
comps = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:date];
//NSCalendarUnitYear:iOS8之前用NSYearCalendarUnit,NSCalendarUnitMonth,NSCalendarUnitDay同理
NSDateComponents *adcomps = [[NSDateComponents alloc] init];
[adcomps setYear:year];
[adcomps setMonth:month];
[adcomps setDay:day];
return [calendar dateByAddingComponents:adcomps toDate:date options:0];
}