一、计算当前日期是周几
1、定义一个枚举:
/**
* 判断日期是当月的星期几
*/
typedef NS_ENUM(NSUInteger, LDYDateWeekType) {
/**
* 星期一
*/
LDYDateWeekTypeMonday =1,
/**
* 星期二
*/
LDYDateWeekTypeTuesday,
/**
* 星期三
*/
LDYDateWeekTypeWednesday,
/**
* 星期四
*/
LDYDateWeekTypeThursday,
/**
* 星期五
*/
LDYDateWeekTypeFriday,
/**
* 星期六
*/
LDYDateWeekTypeSaturday,
/**
* 星期日
*/
LDYDateWeekTypeSunday
};
2、计算方法:
-(LDYDateWeekType)ldy_weekType {
//计算week数
NSCalendar *calendar = [NSCalendar currentCalendar];
calendar.timeZone = [NSTimeZone defaultTimeZone];
NSInteger week = [[calendar components:NSCalendarUnitWeekday fromDate:self] weekday];
LDYDateWeekType type;
switch (week) {
case 1:
type = LDYDateWeekTypeSunday;
break;
case 2:
type = LDYDateWeekTypeMonday;
break;
case 3:
type = LDYDateWeekTypeTuesday;
break;
case 4:
type = LDYDateWeekTypeWednesday;
break;
case 5:
type = LDYDateWeekTypeThursday;
break;
case 6:
type = LDYDateWeekTypeFriday;
break;
case 7:
type = LDYDateWeekTypeSaturday;
break;
default:
break;
}
return type;
}
二、判断日期是不是指定的周几:
-(BOOL)ldy_isWeekType:(LDYDateWeekType)type {
return [self ldy_weekType] == type ?YES:NO;
}
三、返回到指定日期的当年当月当周的周几的日期:
-(NSDate *)ldy_JupmToWeekType:(LDYDateWeekType)targetType {
LDYDateWeekType currentType = [self ldy_weekType];
if (currentType == targetType) {
return self;
}
NSInteger interval = targetType - currentType;
interval = interval>=0 ? interval:-interval;
return [self ldy_dateByAddingDays:interval];
}