服务器返回时间处理

  • 开发中直接拿来用
服务器返回时间处理_第1张图片
时间处理

服务器返回时间使用

dict[@"jDate"] = @"2015-11-23 13:34:54"
NSDateFormatter *fmt 日期格式化类
[fmt setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
相同格式的时间才可以比较

用法:重写数据模型的gett方法、当显示时就会去计算处理
时间也会动态的改变实时更新,上下滚动,重新传模型,又会调用模型的getter方法

NSCalender、能比较2个时间之间的差值
先把时间转成只有年月日
比较from和self的时间差值——>谁调用就是self

  • 发帖时间,几分钟前,几小时前、昨天、几天前
@implementation MGTopic
- (NSString *)create_time
{
    // 日期格式化类
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = @"yyyy-MM-dd HH:mm";
    NSDate *create = [fmt dateFromString:_create_time];

    
    if (create.isThisYear) { // 今年
        if (create.isToday) { // 今天
            // 从大时间开始判断起
            NSDateComponents *cmps = [create deltaWithNow];
            if (cmps.hour >= 1) { // 1小时前
                return [NSString stringWithFormat:@"%zd小时前", cmps.hour];
            } else if (cmps.minute >= 1) { // 1分钟前
                return [NSString stringWithFormat:@"%zd分钟前", cmps.minute];
            } else {
                return @"刚刚";
            }
            
        } else if (create.isYesterday) { // 昨天
            fmt.dateFormat = @"昨天 HH:mm:ss";
            return [fmt stringFromDate:create];
        } else { // 其他
            fmt.dateFormat = @"MM-dd HH:mm:ss";
            return [fmt stringFromDate:create];
        }
        
    } else { // 不是今年
        return _create_time;
    }
}
@end
  • 判断日期,是否是今天、昨天、………
#import 
@interface NSDate (MGExtension)
/** 是否是今天 */
- (BOOL)isToday;
/** 是否是昨天 */
- (BOOL)isYesterday;
/** 是否是今年 */
- (BOOL)isThisYear;
/** 获得与当前时间的差距 */
- (NSDateComponents *)deltaWithNow;
/** 返回只包含年月日的时间 */
- (NSDate *)dateWithYMD;
@end

@implementation NSDate (MGExtension)
/** 是否为今天,年月日一样 */
- (BOOL)isToday
{
   NSCalendar *calendar = [NSCalendar currentCalendar];
   int unit = NSCalendarUnitDay | NSCalendarUnitMonth |  NSCalendarUnitYear;

    // 1.获得当前时间的年月日
    NSDateComponents *nowCmps = [calendar components:unit fromDate:[NSDate date]];

    // 2.获得self的年月日
    NSDateComponents *selfCmps = [calendar components:unit fromDate:self];
   return
    (selfCmps.year == nowCmps.year) &&
    (selfCmps.month == nowCmps.month) &&
    (selfCmps.day == nowCmps.day);
}

/** 是否是昨天 */
// 先把时分秒去掉,只有年月日的时间,相差多少天
- (BOOL)isYesterday
{
    // 2014-12-31  和 2015-01-01只相差一天
    // 2014-05-01 18:45:56
    // 2014-05-01
    NSDate *nowDate = [[NSDate date] dateWithYMD];

    // 2014-04-30 19:23:49
    // 2014-04-30
    NSDate *selfDate = [self dateWithYMD]; 

    // 只关注天数的差别 获得nowDate和selfDate的差距
    NSCalendar *calender = [NSCalendar currentCalendar];
    NSDateComponents *cmps = [calender components:NSCalendarUnitDay fromDate:selfDate toDate:nowDate options:0];
    // 可能相差1 年,差1天—>cmps.day是366天
    return cmps.day == 1;
}

/** 是否为今年 */
- (BOOL)isThisYear
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    int unit = NSCalendarUnitYear;

    // 1.获得当前时间的年月日
    NSDateComponents *nowCmps = [calendar components:unit fromDate:[NSDate date]];
    // 2.获得self的年月日
    NSDateComponents *selfCmps = [calendar components:unit fromDate:self];

    return nowCmps.year == selfCmps.year;
}

/** 获得与当前时间的差距,谁调用谁就是self */
- (NSDateComponents *)deltaWithNow
{
    // 2014-09-10 18:40:30
    // 2014-09-10 19:50:50
    // 1h 10m 20s
    NSCalendar *calender = [NSCalendar currentCalendar];
    int unit = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    return [calender components:unit fromDate:self toDate:[NSDate date] options:0];
}

/** 返回只包含年月日的时间 */
- (NSDate *)dateWithYMD
{
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = @"yyyy-MM-dd";
    NSString *dateStr = [fmt stringFromDate:self];
    return [fmt dateFromString:dateStr];
}
@end

用例:

//  时间戳转化为是刚刚、几分钟前、几小时前、几天前等(与当前时间相比)
+ (NSString *)currentDateWithRerence:(NSTimeInterval)timeStamp {
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timeStamp];
    return [[NSDate date] prettyDateWithReference:confromTimesp];
}

//  时间转化为是刚刚、几分钟前、几小时前、几天前等(自定义时间相比)
- (NSString *)prettyDateWithReference:(NSDate *)reference {
    NSString *suffix = @"前";
    
    float different = [reference timeIntervalSinceDate:self];
    if (different < 0) {
        different = -different;
        suffix = @"前";
    }
    
    // days = different / (24 * 60 * 60), take the floor value
    float dayDifferent = floor(different / 86400);
    
    int days   = (int)dayDifferent;
    int weeks  = (int)ceil(dayDifferent / 7);
    int months = (int)ceil(dayDifferent / 30);
    int years  = (int)ceil(dayDifferent / 365);
    
    // It belongs to today
    if (dayDifferent <= 0) {
        // lower than 60 seconds
        if (different < 60) {
            return @"刚刚";
        }
        
        // lower than 120 seconds => one minute and lower than 60 seconds
        if (different < 120) {
            return [NSString stringWithFormat:@"1分钟%@", suffix];
        }
        
        // lower than 60 minutes
        if (different < 66 * 60) {
            return [NSString stringWithFormat:@"%d分钟%@", (int)floor(different / 60), suffix];
        }
        
        // lower than 60 * 2 minutes => one hour and lower than 60 minutes
        if (different < 7200) {
            return [NSString stringWithFormat:@"1小时%@", suffix];
        }
        
        // lower than one day
        if (different < 86400) {
            return [NSString stringWithFormat:@"%d小时%@", (int)floor(different / 3600), suffix];
        }
    }
    // lower than one week
    else if (days < 7) {
        return [NSString stringWithFormat:@"%d天%@%@", days, days == 1 ? @"" : @"", suffix];
    }
    // lager than one week but lower than a month
    else if (weeks < 4) {
        return [NSString stringWithFormat:@"%d周%@%@", weeks, weeks == 1 ? @"" : @"", suffix];
    }
    // lager than a month and lower than a year
    else if (months < 12) {
        return [NSString stringWithFormat:@"%d月%@%@", months, months == 1 ? @"" : @"", suffix];
    }
    // lager than a year
    else {  
        return [NSString stringWithFormat:@"%d年%@%@", years, years == 1 ? @"" : @"", suffix];
    }  

    return self.description;  
}

//  时间戳转化为当前时间
+ (NSString *)timeIntervalToNSDate:(NSTimeInterval)time {
    //因为时差问题要加8小时 == 28800 sec
    NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:(time)];
    //实例化一个NSDateFormatter对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设定时间格式,这里可以设置成自己需要的格式
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *currentDateStr = [dateFormatter stringFromDate: detaildate];
    return currentDateStr;
}

  • 两日期之间相差几天
- (NSInteger)getDifferenceByDate:(NSString *)date {
    //获得当前时间
    NSDate *now = [NSDate date];
    //实例化一个NSDateFormatter对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设定时间格式
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *oldDate = [dateFormatter dateFromString:date];
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    unsigned int unitFlags = NSDayCalendarUnit;
    NSDateComponents *comps = [gregorian components:unitFlags fromDate:oldDate toDate:now  options:0];
    return [comps day];
}

金额处理

数字大到一定程度,显示具体的无意义,要抽象化、如12万、4.3万
数字为0时,显示中文。

- (void)setButtonTitle:(UIButton *)button count:(NSInteger)count placeholder:(NSString *)placeholder
{
    // 利用placeholder不用新开变量
    if (count > 10000) {
        placeholder = [NSString stringWithFormat:@"%.1f万", count / 10000.0];
    } else if (count > 0) {
        placeholder = [NSString stringWithFormat:@"%zd", count];
    }    
    [button setTitle:placeholder forState:UIControlStateNormal];
}

参考资料

你可能感兴趣的:(服务器返回时间处理)