[iOS]NSDateComponents简单使用

经炜
2023-12-01

获取某月有多少天、某年有多少天、某天是周几、某天在某年中在第几周...

NSDate *newDate = [NSDate date];
NSInteger daysOfMonth = [GABaseObject getDaysOfTheMonthWithDate:newDate];
NSInteger daysOfYear = [GABaseObject getDaysOfTheYearWithDate:newDate];
NSDateComponents *coms = [GABaseObject getDateComponentsWithDate:newDate];
NSString *weekday = [GABaseObject getWeekdayStringWithIndex:coms.weekday];

简单使用

#import "GABaseObject.h"

@implementation GABaseObject

/// 获得日期组件
+ (NSDateComponents *)getDateComponentsWithDate:(NSDate *)theDate {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    // 设置每周的第一天从周几开始,默认每周第一天是周日,这里设置为2表示每周从周一开始
    [calendar setFirstWeekday:2];
    NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond|NSCalendarUnitWeekday|NSCalendarUnitWeekOfYear fromDate:theDate];
    return components;
}

/// 获取某个月的天数
+ (NSInteger)getDaysOfTheMonthWithDate:(NSDate *)theDate {
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSRange range = [calendar rangeOfUnit:NSCalendarUnitDay
                                   inUnit:NSCalendarUnitMonth
                                  forDate:theDate];
    return range.length;
}

/// 获取某年的天数
+ (NSInteger)getDaysOfTheYearWithDate:(NSDate *)theDate {
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSRange range = [calendar rangeOfUnit:NSCalendarUnitDay
                                   inUnit:NSCalendarUnitYear
                                  forDate:theDate];
    return range.length;
}

/// 判断两个时间的大小和间隔。mark = 0判断开始时间是否大于结束时间,mark = 1判断时间间隔
+ (BOOL)judgeIntervalStartDate:(NSDate *)startDate EndDate:(NSDate *)endDate Mark:(NSInteger)mark {
    // 当前日历
    NSCalendar *calendar = [NSCalendar currentCalendar];
    // 需要对比的时间数据
    NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth
    | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    // 对比时间差
    NSDateComponents *dateCom = [calendar components:unit fromDate:startDate toDate:endDate options:0];
    if (mark == 1) {
        if (dateCom.year >= 1 && dateCom.day != 0) {
            return NO;
        }
        return YES;
    } else {
        if (dateCom.year < 0 || dateCom.month < 0 || dateCom.day < 0 || dateCom.hour < 0 || dateCom.minute < 0 || dateCom.second < 0) {
            return NO;
        }
        return YES;
    }
}

/// weekday int转string
+ (NSString *)getWeekdayStringWithIndex:(NSInteger)index {
    NSString *tempWeek = @"";
    switch (index) {
        case 1:
            tempWeek = @"周日";
            break;
        case 2:
            tempWeek = @"周一";
            break;
        case 3:
            tempWeek = @"周二";
            break;
        case 4:
            tempWeek = @"周三";
            break;
        case 5:
            tempWeek = @"周四";
            break;
        case 6:
            tempWeek = @"周五";
            break;
        case 7:
            tempWeek = @"周六";
            break;
        default:
            break;
    }
    return tempWeek;
}

@end

日历单元枚举

typedef NS_OPTIONS(NSUInteger, NSCalendarUnit) {
        NSCalendarUnitEra                = kCFCalendarUnitEra,
        NSCalendarUnitYear               = kCFCalendarUnitYear,
        NSCalendarUnitMonth              = kCFCalendarUnitMonth,
        NSCalendarUnitDay                = kCFCalendarUnitDay,
        NSCalendarUnitHour               = kCFCalendarUnitHour,
        NSCalendarUnitMinute             = kCFCalendarUnitMinute,
        NSCalendarUnitSecond             = kCFCalendarUnitSecond,
        NSCalendarUnitWeekday            = kCFCalendarUnitWeekday,
        NSCalendarUnitWeekdayOrdinal     = kCFCalendarUnitWeekdayOrdinal,
        NSCalendarUnitQuarter            API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0)) = kCFCalendarUnitQuarter,
        NSCalendarUnitWeekOfMonth        API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0)) = kCFCalendarUnitWeekOfMonth,
        NSCalendarUnitWeekOfYear         API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0)) = kCFCalendarUnitWeekOfYear,
        NSCalendarUnitYearForWeekOfYear  API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0)) = kCFCalendarUnitYearForWeekOfYear,
        NSCalendarUnitNanosecond         API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0)) = (1 << 15),
        NSCalendarUnitCalendar           API_AVAILABLE(macos(10.7), ios(4.0), watchos(2.0), tvos(9.0)) = (1 << 20),
        NSCalendarUnitTimeZone           API_AVAILABLE(macos(10.7), ios(4.0), watchos(2.0), tvos(9.0)) = (1 << 21),
};

 

 类似资料: