获取当月中每天是周几
定义方法- (NSArray *)getWeekdaysInCalenderWithDate:(NSString *)dateString
self.allWeekdaysInMonth = [NSMutableArray array];
NSDateFormatter *forma = [[NSDateFormatter alloc] init];
[forma setDateFormat:@"yyyy-MM-dd"];
NSDate *date = [forma dateFromString:dateString];
NSInteger dayCount = [self numberOfDaysInMonth:date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM"];
NSString *dateStr = [formatter stringFromDate:date];
[formatter setDateFormat:@"yyyy-MM-dd"];
for (NSInteger i = 1; i <= dayCount; i++) {
NSString *useStr = [NSString stringWithFormat:@"%@-%ld", dateStr, (long)i];
NSDate *suDate = [formatter dateFromString:useStr];
[self.allWeekdaysInMonth addObject:@([self getweekDayWithDate:suDate])];
}
return self.allWeekdaysInMonth;
获取指定日期是星期几
实现方法- (NSInteger) getweekDayWithDate:(NSDate *) date
NSCalendar * calendar = [[NSCalendar alloc]
initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; // 指定日历的算法
NSDateComponents *comps = [calendar components:NSCalendarUnitWeekday fromDate:date];
return comps.weekday;
获取当月的天数
实现方法- (NSInteger)numberOfDaysInMonth:(NSDate *)monthDate
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSRange range = [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:monthDate];
return range.length;
获取每天的日期详细信息
实现方法- (void)getDateInfoWithDate:(NSDate *)date
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *comps = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitHour|NSCalendarUnitHour| NSCalendarUnitMinute|NSCalendarUnitSecond|NSCalendarUnitWeekOfMonth|NSCalendarUnitWeekday fromDate:date];
_year = comps.year;
_month = comps.month;
_day = comps.day;
_hour = comps.hour;
_minute = comps.minute;
_second = comps.second;
_weekday = comps.weekday;
大致的信息都获取到了,根据这些信息就可以自己定制自己想要的日历样式了