iOS之日历时间的操作

iOS开发中,经常遇到日历时间等的操作,主要涉及NSDate、NSDateFormatter、NSCalendar、NSDateComponents等与时间日期计算的类。开发中经常会遇到,这里记一下,方便以后查询。

获取某个日期的年、月、日

- (NSInteger)day:(NSDate *)date{
    NSDateComponents *components = [[NSCalendar currentCalendar] components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:date];
    return [components day];
}

- (NSInteger)month:(NSDate *)date{
    NSDateComponents *components = [[NSCalendar currentCalendar] components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:date];
    return [components month];
}

- (NSInteger)year:(NSDate *)date{
    NSDateComponents *components = [[NSCalendar currentCalendar] components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:date];
    return [components year];
}

计算某个日期的月份总天数

- (NSInteger)totalDaysInMonth:(NSDate *)date{
    NSRange totalDays = [[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date];
    return totalDays.length;
}

对当前日期,增加给定的秒数,并以此算出新的日期

- (NSDate *)addTimeInterval:(NSTimeInterval)interval toDate:(NSDate *)date {
    return [date dateByAddingTimeInterval:interval];
}

计算两个日期中的间隔秒数差

- (NSTimeInterval)timeIntervalFromDate:(NSDate *)beginDate toDate:(NSDate *)endDate {
    NSTimeInterval timeInterval = [endDate timeIntervalSinceDate:beginDate];
    return timeInterval;
}

日期date转日期格式化的字符串

//formatString格式:yyyy-MM-dd
- (NSString *)dateStringWithDate:(NSDate *)date formatString:(NSString *)formatString {
    NSDateFormatter *format = [[NSDateFormatter alloc]init];
    [format setDateFormat:formatString];
    return [format stringFromDate:date];
}

日期字符串转日期Date

//format格式:yyyy-MM-dd
- (NSDate *)dateWithDateString:(NSString *)dateString formatString:(NSString *)formatString {
    NSDateFormatter *format = [[NSDateFormatter alloc]init];
    [format setDateFormat:formatString];
    return [format dateFromString:dateString];
}


计算date的某天是星期几(这里可以计算当前月份中的某天星期几,比如求出当月一号的星期几,只需要把下面代码中的[comp setDay:[self day:date]];改成comp setDay:1];即可。)

- (NSInteger)weekdayInThisMonth:(NSDate *)date{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    [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:[self day:date]];
    NSDate *firstDayOfMonthDate = [calendar dateFromComponents:comp];
    
    NSUInteger firstWeekday = [calendar ordinalityOfUnit:NSCalendarUnitWeekday inUnit:NSCalendarUnitWeekOfMonth forDate:firstDayOfMonthDate];
    return firstWeekday - 1;
}

对当前日期加上year,month,day后的未来日期。(year,month,day传入正值)。

对当前日期减去year,month,day后的过去日期。(year,month,day传入负值)。

- (NSString *)dateStringAfterYear:(NSInteger)year Months:(NSInteger)month days:(NSInteger)day {
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *component = [[NSDateComponents alloc] init];
    
    [component setYear:year];
    [component setMonth:month];
    [component setDay:day];
    
    NSDate *newDate = [calendar dateByAddingComponents:component toDate:self options:0];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString *dateString = [formatter stringFromDate:newDate];
    return dateString;
}

由上面的做法,同样可以对当前的小时分钟来计算前后的时间。

- (NSString *)dateStringAfterHour:(NSInteger)hour minutes:(NSInteger)minute {
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *component = [[NSDateComponents alloc] init];
    
    [component setHour:hour];
    [component setMinute:minute];
    
    NSDate *newDate = [calendar dateByAddingComponents:component toDate:self options:0];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"HH:mm"];
    NSString *newString = [formatter stringFromDate:newDate];
    return newString;
}

计算给定日期的上一个月,下一个月

- (NSDate *)lastMonth:(NSDate *)date{
    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
    dateComponents.month = -1;
    NSDate *newDate = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:date options:0];
    return newDate;
}

- (NSDate*)nextMonth:(NSDate *)date{
    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
    dateComponents.month = +1;
    NSDate *newDate = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:date options:0];
    return newDate;
}

文章来自 http://skymonkey.cn/

高能广告区

暂无广告哦=^^=。继续看看其它文章吧!