时间戳的生成处理

 1 -(NSString *) FormartTime:(NSDate*) compareDate
 2 {
 3     if( compareDate == nil ) return @"";
 4     
 5     NSTimeInterval  timeInterval = [compareDate timeIntervalSinceNow];
 6     timeInterval = -timeInterval;
 7     long temp = timeInterval;
 8     NSString *result;
 9     
10     if (timeInterval < 60) {
11         if( temp == 0 )
12             result = @"刚刚";
13         else
14             result = [NSString stringWithFormat:@"%d秒前",(int)temp];
15     }
16     else if(( timeInterval/60) <60){
17         result = [NSString stringWithFormat:@"%d分钟前",(int)temp/60];
18     }
19     
20     else if(( temp/86400) <30){
21         
22         NSDateFormatter *date = [[NSDateFormatter alloc] init];
23         [date setDateFormat:@"dd"];
24         NSString *str = [date stringFromDate:[NSDate date]];
25         int nowday = [str intValue];
26         
27         NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
28         [dateFormatter setDateFormat:@"dd"];
29         NSString *strDate = [dateFormatter stringFromDate:compareDate];
30         int day = [strDate intValue];
31         if (nowday-day==0) {
32             //[dateFormatter setDateFormat:@"今天 HH:mm"];
33             [dateFormatter setDateFormat:@"HH:mm"];
34             result =    [dateFormatter stringFromDate:compareDate];
35             
36         }
37         else if(nowday-day==1)
38         {
39             
40             [dateFormatter setDateFormat:@"昨天 HH:mm"];
41             // result =  [dateFormatter stringFromDate:compareDate];
42             result =  @"昨天";
43             
44         }
45         
46         else if( temp < 8 )
47         {
48             if (temp==1) {
49                 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
50                 [dateFormatter setDateFormat:@"昨天HH:mm"];
51                 NSString *strDate = [dateFormatter stringFromDate:compareDate];
52                 result = strDate;
53             }
54             else if(temp == 2)
55             {
56                 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
57                 [dateFormatter setDateFormat:@"前天HH:mm"];
58                 NSString *strDate = [dateFormatter stringFromDate:compareDate];
59                 result = strDate;
60             }
61             
62         }
63         else
64         {
65             NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
66             [dateFormatter setDateFormat:@"MM-dd HH:mm"];
67             NSString *strDate = [dateFormatter stringFromDate:compareDate];
68             result = strDate;
69             
70         }
71     }
72     else
73     {//超过一个月的就直接显示时间了
74         NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
75         [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
76         NSString *strDate = [dateFormatter stringFromDate:compareDate];
77         result = strDate;
78     }
79     
80     /*
81      else if((temp = (temp/(3600*24))/30) <12){
82      result = [NSString stringWithFormat:@"%d个月前",(int)temp];
83      }
84      else{
85      temp = temp/12;
86      result = [NSString stringWithFormat:@"%d年前",(int)temp];
87      }
88      */
89     
90     return  result;
91 }

 

posted @ 2016-01-12 10:30  懒懒初阳  阅读(405)  评论(0编辑  收藏  举报