Nsstring * str = [NSString stringWithFormat:@"%@-%@",beginString,endString];
Nslog(@"%@",str);// 年 月 周 的开始结束日期;
// 获得当前日期的上个月的日期
- (void)getBeformouthData
{
NSInteger year,month,day;
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *now = [NSDate date];;
NSDateComponents *comps = [[NSDateComponents alloc] init];
NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
comps = [calendar components:unitFlags fromDate:now];
year = [comps year];
month = [comps month];
day = [comps day];
NSLog(@"现在是:%d年%d月%d日 ",year,month,day);
// month获取现在的月份,然后 判断是上个月份是否为12月
if (month!=1) {
// 如果mouth!=1,那么上个月月初为1号,年份不变
NSLog(@"上个月月初:%d-%d-1 ",year,month-1);
// 处理上个月份为2月的特殊情况,
if (month!=3) {
// 如果上个月的月份为 4 6 9 11 月份,他们月末是30号
if (month == 5 || month==7 || month ==10 || month ==12) {
NSLog(@"上个月月末:%d-%d-30 ",year,month-1);
}
else {
NSLog(@"上个月月末:%d-%d-31 ",year,month-1);
}
}
// 如果本月是3月份,上个月为为2月份,闰年2月月末29天,非闰年28天
else {
if ((year%4==0 && year%100!=0) || year%400==0) {
NSLog(@"上个月月末:%d-%d-29 ",year,month-1);
}
else {
NSLog(@"上个月月末:%d-%d-28 ",year,month-1);
}
}
}
// 如果当前月份是1月,上个月份就是12月 year-1,month=12
else {
NSLog(@"上个月月初为 %d-12-1",year-1);
NSLog(@"上个月月末为 %d-12-31",year-1);
}
}
//获取当前日期的上周一和上周日的时间
-(NSString *)getWeekTime
{
NSDate *nowDate = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comp = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | NSDayCalendarUnit fromDate:nowDate];
//获取今天周几
NSInteger weekDay = [comp weekday]-1;
//获取今天是几号
NSInteger day = [comp day];
NSLog(@"今天周 %d ==========今天%d号========",weekDay,day);
//计算当前日期和本周的周一和周日相差天数
long firstOffset,lastOffset;
if(weekDay == 1)
{
firstOffset = -6;
lastOffset = 0;
}else{
firstOffset = [calendar firstWeekday] - weekDay-7 ;
lastOffset = -weekDay;
}
NSLog(@"firstOffset %ld=======last %ld",firstOffset,lastOffset);
//在当前日期基础上加上差的天数
NSDateComponents *firstDayComp = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:nowDate];
[firstDayComp setDay:day + firstOffset];
NSDate *firstDayOfWeek = [calendar dateFromComponents:firstDayComp];
NSDateComponents *lastDayComp = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:nowDate];
[lastDayComp setDay:day + lastOffset];
NSDate *lastDayOfWeek = [calendar dateFromComponents:lastDayComp];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd"];
NSString *firstDay = [formatter stringFromDate:firstDayOfWeek];
NSString *lastDay = [formatter stringFromDate:lastDayOfWeek];
NSLog(@"%@=======%@",firstDay,lastDay);
//上周的周一 和周日日期
NSString *dateStr = [NSString stringWithFormat:@"%@%@",firstDay,lastDay];
NSLog(@"date=======%@",dateStr);
/**
* firstDay 本周周一
lastDay 本周周末
*/
return dateStr;
}
NSTimeInterval secondsPerDay = 24 * 60 * 60;
//昨天时间
NSDate *yesterday = [[NSDate alloc] initWithTimeIntervalSinceNow:-secondsPerDay];
NSString * yestodayStr = [[NSString stringWithFormat:@"%@",yesterday] substringToIndex:10];
//前天时间
NSDate *beforeyesterday = [[NSDate alloc] initWithTimeIntervalSinceNow:-secondsPerDay*2];
NSString * beforeyesterdayStr = [[NSString stringWithFormat:@"%@",beforeyesterday] substringToIndex:10];