OC: NSCalendar+MaxMethod

2018-12-13  本文已影响1人  一欧Yiou

.h文件

#import <Foundation/Foundation.h>

@interface NSCalendar (MaxMethod)

+ (NSInteger)getYearWithDate:(NSDate *)date;
+ (NSInteger)getMonthWithDate:(NSDate *)date;
+ (NSInteger)getDayWithDate:(NSDate *)date;
+ (NSInteger)getWeekWithDate:(NSDate *)date;
+ (NSString *)getWeekdayStrWithDate:(NSDate *)date;

@end

.m文件

#import "NSCalendar+MaxMethod.h"

@implementation NSCalendar (MaxMethod)

+ (NSInteger)getYearWithDate:(NSDate *)date {
    NSCalendar *calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
    //year
    return [calendar component:NSCalendarUnitYear fromDate:date];
}

+ (NSInteger)getMonthWithDate:(NSDate *)date {
    NSCalendar *calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
    //month
    return [calendar component:NSCalendarUnitMonth fromDate:date];
}

+ (NSInteger)getDayWithDate:(NSDate *)date {
    NSCalendar *calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
    //day
    return [calendar component:NSCalendarUnitDay fromDate:date];
}

+ (NSInteger)getWeekWithDate:(NSDate *)date {
    NSCalendar *calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
    //week
    return [calendar component:NSCalendarUnitWeekday fromDate:date];
}

+ (NSString *)getWeekdayStrWithDate:(NSDate *)date {
    NSString *week = @"";
    NSCalendar *calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
    //week
    switch ([calendar component:NSCalendarUnitWeekday fromDate:date]) {
        case 1:
//            week = @"日曜日";
            week = @"星期日";
            break;
            
        case 2:
//            week = @"月曜日";
            week = @"星期一";
            break;
            
        case 3:
//            week = @"火曜日";
            week = @"星期二";
            break;
            
        case 4:
//            week = @"水曜日";
            week = @"星期三";
            break;
            
        case 5:
//            week = @"木曜日";
            week = @"星期四";
            break;
            
        case 6:
//            week = @"金曜日";
            week = @"星期五";
            break;
            
        case 7:
//            week = @"土曜日";
            week = @"星期六";
            break;
            
        default:
//            week = @"日曜日";
            week = @"星期日";
            break;
    }
    return week;
}
上一篇下一篇

猜你喜欢

热点阅读