iOS-經過日曆獲取某一天

 //獲得當前的時間
                NSDate * mydate = [NSDate date];
                NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
                [dateFormatter setDateFormat:@"yyyy-MM-dd"];
                NSString *currentDateStr = [dateFormatter stringFromDate:[NSDate date]];
//                NSLog(@"---當前的時間的字符串 =%@",currentDateStr);component

//建立日曆
                NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
                NSDateComponents *comps = nil;
                comps = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitMonth fromDate:mydate];
                NSDateComponents *adcomps = [[NSDateComponents alloc] init];
                [adcomps setYear:0];   //年
                [adcomps setMonth:-3];//月  (-3 表明 三個月前的一天)
                [adcomps setDay:0];     //日
                NSDate *newdate = [calendar dateByAddingComponents:adcomps toDate:mydate options:0];
                NSString *beforDate = [dateFormatter stringFromDate:newdate];
//                NSLog(@"過去三個月------------------        %@",beforDate);orm

相關文章
相關標籤/搜索