NSString *bill_timeStr=@"2016-08-11 12:12:47";
算法
NSDate *date1=[self datejishuangYear:0 Month:0 Day:1 withData:bill_timeStr];//計算兩個以後的時間.net
/**
* 時間算法
*
* @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];
}component