iOS 关于日期的方法

#pragma mark 日期字符串按照指定的格式转换成日期
+ (NSDate *)dateFromString:(NSString *)string withFormat:(NSString *)format {
    NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
    [inputFormatter setDateFormat:format];
    NSDate *date = [inputFormatter dateFromString:string];
    
    return date;
}

#pragma mark 日期按照指定的格式转换成字符串
+ (NSString *)stringFromDate:(NSDate *)date withFormat:(NSString *)format {
    NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
    [outputFormatter setDateFormat:format];
    NSString *dateString = [outputFormatter stringFromDate:date];
    
    return dateString;
}

#pragma mark 获取年
- (NSInteger)getYear {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *dayComponents = [calendar components:(NSCalendarUnitYear) fromDate:self];
    return [dayComponents year];
}

#pragma mark 获取月
- (NSInteger)getMonth {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *dayComponents = [calendar components:(NSCalendarUnitMonth) fromDate:self];
    return [dayComponents month];
}

#pragma mark 获取日
- (NSInteger)getDay {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *dayComponents = [calendar components:(NSCalendarUnitDay) fromDate:self];
    return [dayComponents day];
}

#pragma mark 获取时
- (NSInteger)getHour {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *dayComponents = [calendar components:(NSCalendarUnitHour) fromDate:self];
    return [dayComponents hour];
}

#pragma mark 获取分
- (NSInteger)getMinute {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *dayComponents = [calendar components:(NSCalendarUnitMinute) fromDate:self];
    return [dayComponents minute];
}

#pragma mark 获取秒
- (NSInteger)getSecond {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *dayComponents = [calendar components:(NSCalendarUnitSecond) fromDate:self];
    return [dayComponents second];
}

#pragma mark 获取当前月份的天数
+ (NSInteger)getNumberOfDaysInMonth {
    
    NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

    NSRange monthRange = [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:[NSDate date]];
    
    return monthRange.length;
}

#pragma mark 获取指定月份的天数
+ (NSInteger)getNumberOfDaysInMonthWithDateString:(NSString *)dateString {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
//    此处根据需求改对应的日期格式
    [dateFormatter setDateFormat:@"yyyy-MM"];
    NSDate *date = [dateFormatter dateFromString:dateString];
    
    NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    //    通过该方法计算特定日期月份的天数
    NSRange monthRange = [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date];
    
    return monthRange.length;
}

#pragma mark 获取指定日期为星期几
+ (NSString *)weekdayStringFromDate:(NSDate *)inputDate {
    NSArray *weekdays = [NSArray arrayWithObjects: [NSNull null], @"星期日", @"星期一", @"星期二", @"星期三", @"星期四", @"星期五", @"星期六", nil];
    NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/BeiJing"];
    [calendar setTimeZone: timeZone];
    NSCalendarUnit calendarUnit = NSCalendarUnitWeekday;
    NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:inputDate];
    return [weekdays objectAtIndex:theComponents.weekday];
}

#pragma mark 获取本周指定天的日期
- (NSString *)getDateInWeek:(int)day {
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *comp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitWeekday | NSCalendarUnitDay fromDate:self];
//    获取今天是星期几
    NSInteger weekDay = [comp weekday];
//    获取今天是第几号
    NSInteger currentDay = [comp day];;
    
//    计算当前日期和本周的星期一相差天数
    long firstDiff = weekDay == 1 ? -6 : [calendar firstWeekday] - weekDay + 1;

//    在当前日期(去掉时分秒)基础上加上差的天数
    NSDateComponents *firstDayComp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay  fromDate:self];
    [firstDayComp setDay:currentDay + firstDiff];
    NSDate *firstDayOfWeek = [calendar dateFromComponents:firstDayComp];
    
    if (day == 0 || day > 7) {
        day = 7;
    }
    NSDate *correctDate = [NSDate dateWithTimeInterval:(day - 1) * 24 * 60 * 60 sinceDate:firstDayOfWeek];//后day天
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"MM-dd"];
    NSString *dayString = [formatter stringFromDate:correctDate];

    return dayString;
}

#pragma mark 获取当前日期前后N天的日期
- (NSDate *)getDateWithDay:(int)day {
    
    return [NSDate dateWithTimeInterval:day * 24 * 60 * 60 sinceDate:self];
}

#pragma mark 获取当前日期前后N月的日期
- (NSDate *)getDateWithMonth:(int)month {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *componentsToAdd = [[NSDateComponents alloc] init];
    [componentsToAdd setMonth:month];
    NSDate *dateAfterMonth = [calendar dateByAddingComponents:componentsToAdd toDate:self options:0];
    
    return dateAfterMonth;
}

你可能感兴趣的:(iOS 关于日期的方法)