获取这个指定日期的上月的月初和月末时间


“代码”类NSInteger年,月,日,NSCalendar *日历=[[NSCalendar alloc] initWithCalendarIdentifier NSGregorianCalendar):;现在NSDate *=[NSDate日期];;NSDateComponents *比较=[[NSDateComponents alloc] init);NSInteger unitFlags=NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;比较=[日历组件:unitFlags fromDate:现在);年=(比较一年);月=(比较月);一天=(比较天);NSLog (@"现在是:% d年% d月% d日“,年,月,日);//月获取现在的月份,然后判断是上个月份是否为12月如果(月!=1){//如果嘴!=1,那么上个月月初为1号,年份不变NSLog (@"上个月月初:% d年% d月1日“,一年,第1个月);//处理上个月份为2月的特殊情况,如果(月!=3){//如果上个月的月份为4 6 9月11份,他们月末是30号如果(5月==| | 7月==| | 10月==| |月==12){NSLog (@"上个月月末:% d年% d月30日“,一年,第1个月);其他}{NSLog (@"上个月月末:% d年% d月31日“,一年,第1个月);}}//如果本月是3月份,上个月为为2月份,闰年2月月末29天,非闰年28天其他{如果(年% 4==0,,100年% !=0)| | 400年%==0){NSLog (@"上个月月末:% d年% d月29日“,一年,第1个月);其他}{NSLog (@"上个月月末:% d年% d月28日“,一年,第1个月);}}}//如果当前月份是1月,上个月份就是12月1年,月=12其他{NSLog (@"上个月月初为% d年12月1日“,1年);NSLog (@"上个月月末为% d年12月31日“,1年);}








获取这个指定日期的上月的月初和月末时间