码迷,mamicode.com
首页 > 移动开发 > 详细

ios-时间换算

时间:2014-09-24 20:47:27      阅读:263      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   ar   for   div   

经常会遇到时间转换的,在此收藏一个时间换算的方法?

bubuko.com,布布扣
 1 #pragma mark 时间换算
 2 
 3 + (NSString *)setcreateTime:(NSString *)str
 4 {
 5     //yyyy-MM-dd- HH:MMss"];
 6 
 7     
 8     NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
 9     
10     //[formatter setDateStyle:NSDateFormatterMediumStyle];
11     
12     //[formatter setTimeStyle:NSDateFormatterShortStyle];
13     
14     [formatter setDateFormat:@"yyyy"];
15     
16     [formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"CCT"]];
17     
18     NSDate *date = [NSDate dateWithTimeIntervalSince1970:[str doubleValue]];
19     
20     NSString *dateStr = [formatter stringFromDate:date];
21     
22     //本地时间
23     NSDate * senddate=[NSDate date];
24     NSDateFormatter * dateformatter=[[NSDateFormatter alloc] init];
25     [dateformatter setDateFormat:@"YYYY"];
26     NSString * locationString=[dateformatter stringFromDate:senddate];
27  
28     //[dateformatter setDateFormat:@"YYYY-MM-dd-HH-mm-ss"];
29     
30     //判断创建时间 与 本地时间 是否同一年
31     if ([dateStr isEqualToString:locationString]) {
32         [formatter setDateFormat:@"MM-dd HH:mm"];
33     }else{
34         [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
35     }
36     
37     date = [NSDate dateWithTimeIntervalSince1970:[str doubleValue]];
38     dateStr = [formatter stringFromDate:date];
39     
40     return [self compareCurrentTime:date :dateStr];
41 }
42 
43 + (NSString *) compareCurrentTime:(NSDate*) compareDate :(NSString *)str
44 {
45     NSTimeInterval  timeInterval = [compareDate timeIntervalSinceNow];
46     timeInterval = -timeInterval;
47     long temp = 0;
48     NSString *result;
49     if (timeInterval < 60) {
50         if (timeInterval > 1) {
51             result = [NSString stringWithFormat:@"%d秒前",(int)timeInterval+1];
52         }else if (timeInterval < -160){
53             result = str;
54         }
55         else{
56             result = [NSString stringWithFormat:@"%d秒前",1];
57         }
58         
59     }
60     else if((temp = timeInterval/60) <60){
61         result = [NSString stringWithFormat:@"%ld分钟前",temp];
62     }else{
63         result = str;
64     }
65     
66 
67     
68     return  result;
69 }
View Code

 

ios-时间换算

标签:style   blog   http   color   io   os   ar   for   div   

原文地址:http://www.cnblogs.com/xm5mao/p/3991202.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!