zoukankan      html  css  js  c++  java
  • ios-时间换算

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

     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
  • 相关阅读:
    微服务简介
    docker跨主机通信扁平化网络的设计与实现
    docker学习ppt
    docker原理介绍
    我的博客搬家啦
    响应式之像素和viewport
    换行+省略号
    ITerms2在mac系统下的安装和配色,并和go2shell关联
    利其器之webstorm快捷键
    React入门 (2)—实现微博展示列表
  • 原文地址:https://www.cnblogs.com/xm5mao/p/3991202.html
Copyright © 2011-2022 走看看