时间NSDate分类整理

以前新工作在做邮箱app项目,里面新做了一个日程模块,用了很多日期出来分类,有很多对日期特殊处理.这里贡献出来.日期处理特别需要处理的是尽量用系统的类,不要转成日期字符串再比较,日期转字符串特别耗费性能.

//是否是一周内的时间 7天内
- (BOOL)isWeak{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *cmps = [calendar components:NSCalendarUnitWeekdayOrdinal fromDate:self toDate:[NSDate date] options:0];
    return  cmps.weekdayOrdinal == 0;
}

//上下午时间显示 上午9:00
- (NSString *)currentHalfDay{
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    format.AMSymbol =  @"上午"; 
    format.PMSymbol = @"下午";
    format.dateFormat = @"ah:mm";
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];//zh_CN  en_US_POSIX
    [format setLocale:locale];
    NSString *timeStr = [format stringFromDate:self];
    return timeStr;
}

//一天时间取00:00
- (NSDate *)zeroOfDate
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    return [calendar startOfDayForDate:self];
}

// 将秒刻度清零
- (NSDate *)dateWithZeroSeconds
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute;
    NSDateComponents *components = [calendar components:unit fromDate:self];
    NSDate *date = [calendar dateFromComponents:components];
    
    return date;
}

//星期几
- (NSString *)getWeakDay:(NSDate *)date{
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = @"EEE";
    return [fmt stringFromDate:self];
}

 //计算天数差时候,比如 今23.59 和明天 01:00算差一天,要按当地时间计算, 这样就得处理由于时区差导致的问题
- (NSString *)differentDayWithNow
{
一个月内,判断本地化天数差可以这样
    //本地时间天数判断差几天
     NSCalendar *calendar =  [NSCalendar currentCalendar];
      int unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay ;
      NSString *days = @""; //本地化天数差

      NSDateComponents *selfCmps = [calendar components:unit fromDate:self];
      NSDateComponents *nowCmps = [calendar components:unit fromDate:[NSDate date]];
     days =  [NSString stringWithFormat:@"%ld",labs(selfCmps.day - nowCmps.day)];

/*  大于一个月,后上面方法就不适用了,得用下面的方法
      NSDateComponents *cmps =  [calendar components:NSCalendarUnitDay fromDate:self toDate:[NSDate date] options:0];
      NSDateFormatter *format = [[NSDateFormatter alloc] init];
      format.dateFormat = @"HH";
      NSString *timeStr = [format stringFromDate:self];
      NSTimeInterval timeZoneOffset = [[NSTimeZone defaultTimeZone] secondsFromGMT];

       if ([timeStr intValue] <= timeZoneOffset/(60*60)) {
            days =  [NSString stringWithFormat:@"%ld",labs(cmps.day) + 1];
        }else{
            days =  [NSString stringWithFormat:@"%ld",labs(cmps.day)];
         }
*/
}


// 判断是否是同一天
- (BOOL)isSameDay:(NSDate *)date
{
    NSCalendar *calendar =  [NSCalendar currentCalendar];
    int unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay ;
    NSDateComponents *selfCmps = [calendar components:unit fromDate:self];
    NSDateComponents *theCmps = [calendar components:unit fromDate:date];
    
    return
    (selfCmps.year == theCmps.year) &&
    (selfCmps.month == theCmps.month) &&
    (selfCmps.day == theCmps.day);

}

//是否是今年
- (BOOL)isInThisYear{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    BOOL isInThisYear = [calendar isDate:self equalToDate:[NSDate date] toUnitGranularity:(NSCalendarUnitYear)];
    return  isInThisYear;
}


//天数时间差
- (NSString *)differentDayFromStartDate:(NSDate *)startDate
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    int unit = NSCalendarUnitDay ;
    NSDateComponents *cmps =  [calendar components:unit fromDate:self toDate:startDate options:0];
    
    NSString *dateDiff = @"";
    if (cmps.day < 0) { //self时间在startDate时间以后几天
        dateDiff =  [NSString stringWithFormat:@"%ld",labs(cmps.day)];
        
    }else if(cmps.day == 0){
        dateDiff = @"0";
    }else{//self时间在startDate时间之前几天
        dateDiff =  [NSString stringWithFormat:@"%ld",(long)-(cmps.day)];
    }
    
    return dateDiff;
}

//小时时间差
- (NSString *)differentHourFromStartDate:(NSDate *)startDate
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    int unit = NSCalendarUnitHour ;
    NSDateComponents *cmps = [calendar components:unit fromDate:self toDate:startDate options:0];
    NSString *dateDiff = @"";
    
    if (cmps.hour < 0) { //结束时间在开始时间以后
        dateDiff =  [NSString stringWithFormat:@"%ld",labs(cmps.hour)];
        
    }else if(cmps.hour == 0){
        dateDiff = @"0";
    }else{
        dateDiff =  [NSString stringWithFormat:@"%ld",(long)-(cmps.hour)];
    }
    
    return dateDiff;
}

//分钟时间差    isremainder = yes 求余数 %60
- (NSString *)differentMinFromStartDate:(NSDate *)startDate isRemainder:(BOOL)isRemainder{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    int unit = NSCalendarUnitMinute ;
    NSDateComponents *cmps = [calendar components:unit fromDate:self toDate:startDate options:0];
    NSString *dateDiff = @"";
    NSInteger mins = labs(cmps.minute);
    if (isRemainder == YES) {
        mins = labs(cmps.minute) % 60;
    }
    if (cmps.minute < 0) { //结束时间在开始时间以后
        dateDiff =  [NSString stringWithFormat:@"%ld",(long)mins];
        
    }else if(cmps.minute == 0){
        dateDiff = @"0";
    }else{
        dateDiff =  [NSString stringWithFormat:@"%ld",(long)-mins];
    }
    return dateDiff;
}




你可能感兴趣的:(时间NSDate分类整理)