p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #000000; background-color: rgba(160, 223, 163, 0.15) }
p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #3e1e81; background-color: rgba(160, 223, 163, 0.15) }
p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #008400; background-color: rgba(160, 223, 163, 0.15) }
p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; background-color: rgba(160, 223, 163, 0.15); min-height: 14.0px }
p.p5 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #000000; background-color: rgba(160, 223, 163, 0.15); min-height: 21.0px }
p.p6 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #d12f1b; background-color: rgba(160, 223, 163, 0.15) }
p.p7 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #703daa; background-color: rgba(160, 223, 163, 0.15) }
span.s1 { color: #ba2da2 }
span.s2 { color: #703daa }
span.s3 { color: #3e1e81 }
span.s4 { color: #000000 }
span.s5 { color: #d12f1b }
span.s6 { color: #31595d }
span.s7 { color: #272ad8 }
- (void)getQuarterBeginAndEndWith:(NSDate *)currentDate{
NSDate *nowDate = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday fromDate:nowDate];
/**获取当前的月份*/
NSInteger curentMonth = [comp month];
NSString * currentYear = [NSString stringWithFormat:@"%ld",[comp year]];
NSString * beginTime = [self getQuarterStartDateWithYear:currentYear currentMonth:curentMonth];
NSString * endTime = [self getQuarterEndDateWithYear:currentYear currentMonth:curentMonth];
NSString * str = [NSString stringWithFormat:@"季度开始时间 == %@ 季度结束时间 == %@",beginTime,endTime];
NSLog(@"%@",str);
}
//获得某季度的开始日期
- (NSString *)getQuarterStartDateWithYear:(NSString *)paraYear currentMonth:(NSInteger)currentMonth {
NSString * beginTime = @"";
if (currentMonth < 4) {
beginTime = [paraYear stringByAppendingString:@"-01-01"];
} else if (currentMonth >= 4 && currentMonth < 7) {
beginTime = [paraYear stringByAppendingString:@"-04-01"];
} else if (currentMonth >= 7 && currentMonth < 10) {
beginTime = [paraYear stringByAppendingString:@"-07-01"];
} else if (currentMonth >= 10 && currentMonth < 12) {
beginTime = [paraYear stringByAppendingString:@"-10-01"];
}
return beginTime;
}
//获得某季度的结束日期
- (NSString *)getQuarterEndDateWithYear:(NSString *)paraYear currentMonth:(NSInteger)currentMonth {
NSString * endTime = @"";
if (currentMonth < 4) {
endTime = [paraYear stringByAppendingString:@"-03-31"];
} else if (currentMonth >= 4 && currentMonth < 7) {
endTime = [paraYear stringByAppendingString:@"-06-30"];
} else if (currentMonth >= 7 && currentMonth < 10) {
endTime = [paraYear stringByAppendingString:@"-09-30"];
} else if (currentMonth >= 10 && currentMonth < 12) {
endTime = [paraYear stringByAppendingString:@"-12-31"];
}
return endTime;
}
- (void)getWeekBeginAndEndWith:(NSDate *)newDate{
NSDate *nowDate = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday fromDate:nowDate];
// 获取今天是周几
NSInteger weekDay = [comp weekday];
/**获取当前几个月*/
NSInteger monthDay = [comp month];
NSLog(@"%ld",monthDay);
// 获取几天是几号
NSInteger day = [comp day];
NSLog(@"%ld----%ld",weekDay,day);
// 计算当前日期和本周的星期一和星期天相差天数
long firstDiff,lastDiff;
// weekDay = 1;
if (weekDay == 1)
{
firstDiff = -6;
lastDiff = 0;
}
else
{
firstDiff = [calendar firstWeekday] - weekDay + 1;
lastDiff = 8 - weekDay;
}
NSLog(@"firstDiff: %ld lastDiff: %ld",firstDiff,lastDiff);
// 在当前日期(去掉时分秒)基础上加上差的天数
NSDateComponents *firstDayComp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:nowDate];
[firstDayComp setDay:day + firstDiff];
NSDate *firstDayOfWeek = [calendar dateFromComponents:firstDayComp];
NSDateComponents *lastDayComp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:nowDate];
[lastDayComp setDay:day + lastDiff];
NSDate *lastDayOfWeek = [calendar dateFromComponents:lastDayComp];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MM月dd日"];
NSString *firstDay = [formatter stringFromDate:firstDayOfWeek];
NSString *lastDay = [formatter stringFromDate:lastDayOfWeek];
NSLog(@"%@=======%@",firstDay,lastDay);
NSString *dateStr = [NSString stringWithFormat:@"%@-%@",firstDay,lastDay];
NSLog(@"%@",dateStr);
}
- (void)getMonthBeginAndEndWith:(NSDate *)newDate{
if (newDate == nil) {
newDate = [NSDate date];
}
double interval = 0;
NSDate *beginDate = nil;
NSDate *endDate = nil;
NSCalendar *calendar = [NSCalendar currentCalendar];
[calendar setFirstWeekday:2];
//设定周一为周首日
BOOL ok = [calendar rangeOfUnit:NSCalendarUnitMonth startDate:&beginDate interval:&interval forDate:newDate]; //分别修改为 NSDayCalendarUnit NSWeekCalendarUnit NSYearCalendarUnit
if (ok) {
endDate = [beginDate dateByAddingTimeInterval:interval-1];
}else {
return;
}
NSDateFormatter *myDateFormatter = [[NSDateFormatter alloc] init];
[myDateFormatter setDateFormat:@"yyyy.MM.dd"];
NSString *beginString = [myDateFormatter stringFromDate:beginDate];
NSString *endString = [myDateFormatter stringFromDate:endDate];
NSString *s = [NSString stringWithFormat:@"%@-%@",beginString,endString];
NSLog(@"%@",s);
}
原文地址:https://www.cnblogs.com/Jackie-subDai/p/9552540.html