美文网首页
iOS 写日历专用 NSDate分类

iOS 写日历专用 NSDate分类

作者: 彼岸花下的暗影 | 来源:发表于2019-05-23 14:59 被阅读0次

.h文件

@interface NSDate (XZCalendar)

#pragma mark - 获取日
+ (NSInteger)day:(NSDate *)date;
#pragma mark - 获取月
+ (NSInteger)month:(NSDate *)date;
#pragma mark - 获取年
+ (NSInteger)year:(NSDate *)date;
#pragma mark - 获取当月第一天周几
+ (NSInteger)firstWeekdayInThisMonth:(NSDate *)date;
#pragma mark - 获取当前月有多少天
+ (NSInteger)totaldaysInMonth:(NSDate *)date;

/**
 获取与当前月相差数月的date
 @param date 已知的date 根据这个date获取其他月
 @param poor 与date的月份差距  (上月为负,下月为正)
 */
+ (NSDate *)getNewDateWithDate:(NSDate *)date poorCount:(NSInteger)poor;


/**
 判断两个date是否同月

 @param date1 第一个date
 @param date2 第二个date
 @return yes/no
 */
+ (BOOL)isSameDate:(NSDate *)date1 date2:(NSDate *)date2;

@end

.m 文件

@implementation NSDate (XZCalendar)

#pragma mark -- 获取日
+ (NSInteger)day:(NSDate *)date{
    NSDateComponents *components = [[NSCalendar currentCalendar] components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:date];
    return components.day;
}

#pragma mark -- 获取月
+ (NSInteger)month:(NSDate *)date{
    NSDateComponents *components = [[NSCalendar currentCalendar] components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:date];
    return components.month;
}

#pragma mark -- 获取年
+ (NSInteger)year:(NSDate *)date{
    NSDateComponents *components = [[NSCalendar currentCalendar] components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:date];
    return components.year;
}

#pragma mark -- 获得当前月份第一天星期几
+ (NSInteger)firstWeekdayInThisMonth:(NSDate *)date{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    //设置每周的第一天从周几开始,默认为1,从周日开始
    [calendar setFirstWeekday:1];//1.Sun. 2.Mon. 3.Thes. 4.Wed. 5.Thur. 6.Fri. 7.Sat.
    NSDateComponents *comp = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:date];
    [comp setDay:1];
    NSDate *firstDayOfMonthDate = [calendar dateFromComponents:comp];
    NSUInteger firstWeekday = [calendar ordinalityOfUnit:NSCalendarUnitWeekday inUnit:NSCalendarUnitWeekOfMonth forDate:firstDayOfMonthDate];
    //若设置从周日开始算起则需要减一,若从周一开始算起则不需要减
    return firstWeekday - 1;
}

#pragma mark -- 获取当前月共有多少天
+ (NSInteger)totaldaysInMonth:(NSDate *)date{
    NSRange daysInLastMonth = [[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date];
    return daysInLastMonth.length;
}

#pragma mark - 获取其他月的数据
+ (NSDate *)getNewDateWithDate:(NSDate *)date poorCount:(NSInteger)poor{
    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
    dateComponents.month = poor;
    NSDate *newDate = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:date options:0];
    return newDate;
}

// 判断是否是同一月
+ (BOOL)isSameDate:(NSDate *)date1 date2:(NSDate *)date2{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    unsigned unitFlag = NSCalendarUnitYear | NSCalendarUnitMonth;
    NSDateComponents *comp1 = [calendar components:unitFlag fromDate:date1];
    NSDateComponents *comp2 = [calendar components:unitFlag fromDate:date2];
    return (([comp1 month] == [comp2 month]) && ([comp1 year] == [comp2 year]));
}

@end

相关文章

网友评论

      本文标题:iOS 写日历专用 NSDate分类

      本文链接:https://www.haomeiwen.com/subject/qyzvzqtx.html