好几次在项目里面做到关于日历的需求,总结了几个基本常用的方法如下:
// 获取某月某一天的公立日期
- (NSDate *)dateOfMonthWithDay:(NSInteger)day WithDate:(NSDate *)date{
NSCalendar *calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *components = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:date];
[components setDay:day];
// 频繁调用 [NSCalendar currentCalendar] 可能存在性能问题
// NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *dateOfDay = [self.currentCalendar dateFromComponents:components];
return dateOfDay;
}
//获取当前的日
-(NSInteger)dayFromDate:(NSDate *)date {
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *components = [gregorian components:NSCalendarUnitDay fromDate:date];
return [components day];
}
//获取当日是周几
-(NSInteger)weekdayFromDate:(NSDate *)date {
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *components = [gregorian components:NSCalendarUnitWeekday fromDate:date];
return [components weekday];
}
//获取当前月有多少天
- (NSUInteger)numberOfDaysInMonthFromDate:(NSDate *)date
{
// 频繁调用 [NSCalendar currentCalendar] 可能存在性能问题
// NSCalendar *calendar = [NSCalendar currentCalendar];
NSRange range = [self.currentCalendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date];
return range.length;
}
//获得一月的第一天为星期几
-(NSUInteger)firstWeekDayInMonthFromDate:(NSDate *)date {
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
//1.Sun. 2.Mon. 3.Thes. 4.Wed. 5.Thur. 6.Fri. 7.Sat.
[gregorian setFirstWeekday:1];
//[gregorian setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"nl_NL"]];
//Set date to first of month
NSDateComponents *comps = [gregorian components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:date];
[comps setDay:1];
NSDate *newDate = [gregorian dateFromComponents:comps];
NSUInteger firstDay = [gregorian ordinalityOfUnit:NSCalendarUnitWeekday inUnit:NSCalendarUnitWeekOfMonth forDate:newDate];
return firstDay;
}
//2.在调用方法的类中写方法
//上一个月的日期
- (NSDate *)lastMonthDate:(NSDate *)date
{
// NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [self.currentCalendar components:(NSCalendarUnitWeekday | NSCalendarUnitMonth |NSCalendarUnitYear | NSCalendarUnitDay) fromDate:date];
if ([components month] == 1) {
[components setMonth:12];
[components setYear:[components year] - 1];
} else {
[components setMonth:[components month] - 1];
}
[components setDay:1];
NSDate *lastMonth = [self.currentCalendar dateFromComponents:components];
return lastMonth;
}
//下一个月的日期
- (NSDate *)nextMonthDate:(NSDate *)date
{
// NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [self.currentCalendar components:(NSCalendarUnitWeekday|NSCalendarUnitMonth |NSCalendarUnitYear | NSCalendarUnitDay) fromDate:date];
if ([components month] == 12) {
[components setMonth:1];
[components setYear:[components year] + 1];
} else {
[components setMonth:[components month] + 1];
}
[components setDay:1];
NSDate *lastMonth = [self.currentCalendar dateFromComponents:components];
return lastMonth;
}