NSCalendar 功能很強(qiáng)大,多次調(diào)用時(shí)需要考慮性能問題,但必要的使用會(huì)在日期計(jì)算時(shí)事半功倍。
計(jì)算周次所在日期范圍
-(NSString*)timeConversionYear:(NSInteger)year WeakOfYear:(NSInteger)weekofYear
{
//周次的范圍日期 幾月幾日 - 幾月幾日
NSString *weekDate = @"";
//時(shí)間軸 取每一年的六月一號(hào) 沒有特別的含義
NSString *timeAxis = [NSString stringWithFormat:@"%ld-06-01 12:00:00",(long)year];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
//獲得了時(shí)間軸
NSDate *date = [dateFormatter dateFromString:timeAxis];
//日歷類 提供大部分的時(shí)間計(jì)算接口
NSCalendar *calendar = [NSCalendar currentCalendar];
/**這兩個(gè)參數(shù)的設(shè)置影響著周次的個(gè)數(shù)和劃分*****************/
[calendar setFirstWeekday:2]; //設(shè)置每周的開始是星期一
[calendar setMinimumDaysInFirstWeek:7]; //設(shè)置一周至少需要幾天
/****************/
//一個(gè)封裝了具體年月日、時(shí)秒分、周、季度等的類
NSDateComponents *comps = [calendar components:(NSCalendarUnitWeekOfYear | NSCalendarUnitWeekday | NSCalendarUnitWeekdayOrdinal | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay)
fromDate:date];
//時(shí)間軸是當(dāng)前年的第幾周
NSInteger todayIsWeek = [comps weekOfYear];
//第幾周的字符串格式
//NSString *todayIsWeekStr = [NSString stringWithFormat:@"%ld",(long)todayIsWeek];
//獲取時(shí)間軸是星期幾 1(星期天) 2(星期一) 3(星期二) 4(星期三) 5(星期四) 6(星期五) 7(星期六)
NSInteger todayIsWeekDay = [comps weekday];
//得到時(shí)間軸是幾號(hào)
// NSInteger todayIsDay = [comps day];
// 計(jì)算當(dāng)前日期和這周的星期一和星期天差的天數(shù)
//firstDiff 星期一相差天數(shù) 、 lastDiff 星期天相差天數(shù)
long firstDiff,lastDiff;
if (todayIsWeekDay == 1) {
firstDiff = -6;
lastDiff = 0;
}else
{
firstDiff = [calendar firstWeekday] - todayIsWeekDay;
lastDiff = 8 - todayIsWeekDay;
}
NSDate *firstDayOfWeek= [NSDate dateWithTimeInterval:24*60*60*firstDiff sinceDate:date];
NSDate *lastDayOfWeek= [NSDate dateWithTimeInterval:24*60*60*lastDiff sinceDate:date];
long weekdifference = weekofYear - todayIsWeek;
firstDayOfWeek= [NSDate dateWithTimeInterval:24*60*60*7*weekdifference sinceDate:firstDayOfWeek];
lastDayOfWeek= [NSDate dateWithTimeInterval:24*60*60*7*weekdifference sinceDate:lastDayOfWeek];
// NSLog(@"星期一的日期 %@",[dateFormatter stringFromDate:firstDayOfWeek]);
// NSLog(@"星期天的日期 %@",[dateFormatter stringFromDate:lastDayOfWeek]);
NSDateComponents *firstDayOfWeekcomps = [calendar components:(NSCalendarUnitWeekOfYear | NSCalendarUnitWeekday | NSCalendarUnitWeekdayOrdinal | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay)
fromDate:firstDayOfWeek];
NSDateComponents *lastDayOfWeekcomps = [calendar components:(NSCalendarUnitWeekOfYear | NSCalendarUnitWeekday | NSCalendarUnitWeekdayOrdinal | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay)
fromDate:lastDayOfWeek];
NSInteger startMonth = [firstDayOfWeekcomps month];
NSInteger startDay = [firstDayOfWeekcomps day];
NSInteger endmonth = [lastDayOfWeekcomps month];
NSInteger endday = [lastDayOfWeekcomps day];
weekDate = [NSString stringWithFormat:@"%ld.%ld.%ld-%ld.%ld.%ld",year,(long)startMonth,(long)startDay,year,(long)endmonth,(long)endday];
return weekDate;
}
計(jì)算年有多少周
+(NSInteger)weeksInOneYear:(NSInteger)year
{
NSString *timeAxis = [NSString stringWithFormat:@"%ld-12-31 12:00:00",(long)year];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
//獲得了時(shí)間軸
NSDate *date = [dateFormatter dateFromString:timeAxis];
//日歷類 提供大部分的時(shí)間計(jì)算接口
NSCalendar *calendar = [NSCalendar currentCalendar];
[calendar setFirstWeekday:2];
[calendar setMinimumDaysInFirstWeek:7];
// NSRange range = [calendar rangeOfUnit:NSCalendarUnitWeekOfYear inUnit:NSCalendarUnitYear forDate:date];
NSInteger length = [calendar ordinalityOfUnit:NSCalendarUnitWeekOfYear inUnit:NSCalendarUnitYear forDate:date];
return length;
}