zoukankan      html  css  js  c++  java
  • 人性化显示发布时间

    + (NSString *)findendliyTime:(NSString *)dataTime{

        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

        //设置格式 年yyyy 月 MM 日dd 小时hh(HH) 分钟 mm 秒 ss MMM单月 eee周几 eeee星期几 a上午下午

        //与字符串保持一致

        [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

        //现在的时间转换成字符串

        NSDate * nowDate = [NSDate date];

        NSString * noewTime = [formatter stringFromDate:nowDate];

        //参数字符串转化成时间格式

        NSDate * date = [formatter dateFromString:dataTime];

        //参数时间距现在的时间差

        NSTimeInterval time = -[date timeIntervalSinceNow];

        NSLog(@"%f",time);

        //上述时间差输出不同信息

        if (time < 60) {

            return @"刚刚";

            

        }else if (time <3600){

            int minute = time/60;

            NSString * minuteStr = [NSString stringWithFormat:@"%d分钟前",minute];

            return  minuteStr;

            

        }else {

            //如果年不同输出某年某月某日

            if ([[dataTime substringToIndex:4] isEqualToString:[noewTime substringToIndex:4]]) {

                //截取字符串从下标为5开始 2个

                NSRange rangeM = NSMakeRange(5, 2);

                //如果月份不同输出某月某日某时

                if ([[dataTime substringWithRange:rangeM]isEqualToString:[noewTime substringWithRange:rangeM]]) {

                    

                    NSRange rangD = NSMakeRange(8, 2);

                    

                    //如果日期不同输出某日某时

                    if ([[dataTime substringWithRange:rangD]isEqualToString:[noewTime substringWithRange:rangD]]) {

                        NSRange rangeSSD = NSMakeRange(11, 5);

                        NSString * Rstr = [NSString stringWithFormat:@"今日%@",[dataTime substringWithRange:rangeSSD]];

                        return  Rstr;

                    }else{

                        NSRange rangSD = NSMakeRange(5, 5);

                        return [dataTime substringWithRange:rangSD];

                    }

                }else{

                    NSRange rangeSM = NSMakeRange(5,5);

                    return [dataTime substringWithRange:rangeSM];

                }

            }else{

                return [dataTime substringToIndex:10];

            }

        }

    }

  • 相关阅读:
    资源文件 -- 初级应用篇
    Dephi中资源文件的应用
    VC++ 中使用内存映射文件处理大文件
    Delphi中正常窗口的实现
    让程序只运行一个实例
    多条明细(绑定到一个字段)汇总
    在更新之后最好有个判断 看是否更新上 编码码自动生成
    Delphi中有序型
    使用powerDesign画er图 并用 Database --->Generate Database ---> Genarate Script一次性将表结构的脚本导出来。
    .Net中的水晶报表
  • 原文地址:https://www.cnblogs.com/isItOk/p/4680605.html
Copyright © 2011-2022 走看看