zoukankan      html  css  js  c++  java
  • 按照指定格式返回时间戳

    按照指定格式返回时间戳

    //需求: 
    //1,一分钟之内的显示“刚刚” 
    //2,一分钟以上且一个小时之内的,显示“多少分钟前”,例如“5分钟前” 
    //3,1小时以内的字颜色为黄色(以上三点与目前一样) 
    //4,1小时以上三天以内的显示“今天/昨天/前天+具体时间” 
    //5,前天以后的显示"日期+具体时间",如"2月11日 20:19" 
    //数据前提: 
    //1, 提供一个时间串(貌似是这样叫的吧) 
    //方法调用: 
    //1, [类名 timeStrTotimeStamp:时间串]; 
    //方法来源: 
    //1, 通过网络代码进行改造
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    //按照指定格式返回时间 (日期+具体时间",如"2月11日 20:19")
    +(NSString *)speTimewithTimeStr:(NSString *)timeStr{
        NSDateFormatter *formatter = [[[NSDateFormatter alloc]init]autorelease];
        [formatter setDateFormat:@"MM月dd日 HH:mm:ss"];
        NSTimeInterval timeInterval = [timeStr intValue];
        NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timeInterval];
        NSString *str =  [formatter stringFromDate:confromTimesp];
        return str;
    }
     
    //返回时间所在日期当天的准确时间. (20:19)
    +(NSString *)smallTimewithTimeStr:(NSString *)timeStr{
        NSDateFormatter *formatter = [[[NSDateFormatter alloc]init]autorelease];
        [formatter setDateFormat:@" HH:mm:ss"];
        NSTimeInterval timeInterval = [timeStr intValue];
        NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timeInterval];
        NSString *str =  [formatter stringFromDate:confromTimesp];
        return str;
    }
     
     
    //判断两个日期是否是同一天
    +(BOOL)isSameDay:(NSDate*)date1 date2:(NSDate*)date2
    {
        NSCalendar* calendar = [NSCalendar currentCalendar];
        unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit NSDayCalendarUnit;
        NSDateComponents* comp1 = [calendar components:unitFlags fromDate:date1];
        NSDateComponents* comp2 = [calendar components:unitFlags fromDate:date2];
        return [comp1 day]   == [comp2 day] &&
        [comp1 month] == [comp2 month] &&
        [comp1 year]  == [comp2 year];
    }
     
     
    //时间转时间戳
    +(NSString *)timeStrTotimeStamp:(id)timeStr{
        //获取当前时间时间串
        //与获得时间串进行对比
        //根据时间差显示不同内容
        //返回内容
        int nowTime = [[DSUtils timeStamp] intValue];
        int gapTime = nowTime - [timeStr intValue];
        NSString *timeStamp = nil;
        if (gapTime < 60) {
            //一分钟内显示刚刚
            timeStamp = [NSString stringWithFormat:@"刚刚"];
        }else if(60<=gapTime && gapTime<60*60){
            //一分钟以上且一个小时之内的,显示“多少分钟前”,例如“5分钟前”
            timeStamp = [NSString stringWithFormat:@"%i分钟前",gapTime/60];
        }else if (60*60<=gapTime && gapTime<60*60*24*3){
            //1小时以上三天以内的显示“今天/昨天/前天+具体时间”
            NSString *dayStr ;
            int gapDay = gapTime/(60*60*24) ;
            switch (gapDay) {
                case 0:
                {
                    //在24小时内,存在跨天的现象. 判断两个时间是否在同一天内.
                    NSDate *date1 = [NSDate date];
                    NSTimeInterval timeInterval = [timeStr intValue];
                    NSDate *date2 = [NSDate dateWithTimeIntervalSince1970:timeInterval];
                    BOOL idSameDay = [DSUtils isSameDay:date1 date2:date2];
                    if (idSameDay == YES) {
                        dayStr = [NSString stringWithFormat:@"今天"];
                    }else{
                        dayStr = [NSString stringWithFormat:@"昨天"];
                    }
                }
                    break;
                case 1:
                    dayStr = [NSString stringWithFormat:@"昨天"];
                    break;
                case 2:
                    dayStr = [NSString stringWithFormat:@"前天"];
                    break;
                default:
                    break;
            }
    //        timeStamp = [dayStr stringByAppendingString:[DSUtils smallTimewithTimeStr:timeStrs]];
            timeStamp = [NSString stringWithFormat:@"%@%@",dayStr,[DSUtils smallTimewithTimeStr:timeStr]];
        }else{
            //前天以后的显示"日期+具体时间",如"2月11日 20:19"
            timeStamp = [NSString stringWithString:[DSUtils speTimewithTimeStr:timeStr]];
        }
        return [timeStamp copy];
    }
  • 相关阅读:
    UrlRewrite(地址变换)技术在IIS 5.0/ASP平台上面的应用
    Asp.Net页面输出到EXCEL
    [收藏] ASP.NET缓存:方法和最佳实践
    基于.NET的全文索引引擎Lucene.NET
    [ASP.NET]支持up,down以及pageup,pagedown,home,end,Enter键盘操作的DataGrid
    无知所以羞耻
    你相信世界上有心有灵犀的事情吗?
    javascript的日期加减
    2007312
    人应该多向上
  • 原文地址:https://www.cnblogs.com/willbin/p/3638573.html
Copyright © 2011-2022 走看看