iOS 与时间相关的处理

  • 类似朋友圈发布时间显示(刚刚... 多少分钟多少小时前 昨天)
    - (NSString *)transformDateStrToMinutes:(NSString *)dateStr {

        NSDateFormatter *date=[[NSDateFormatter alloc] init];
        [date setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        NSDate *d=[date dateFromString:dateStr];
        NSTimeInterval late=[d timeIntervalSince1970]*1;
    
        //当前时间
        NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
        NSTimeInterval now=[dat timeIntervalSince1970]*1;
    
        //单位是秒
        NSTimeInterval cha=now-late;
        NSString *timeString= @"";
        if (cha / 3600 < 1) {
            //小时
            timeString = [NSString stringWithFormat:@"%f", cha/60];
            //        NSLog(@"timeString%@",timeString);
            timeString = [timeString substringToIndex:timeString.length-7];
            int num= [timeString intValue];
            if (num <= 1) {
                 timeString = [NSString stringWithFormat:@"刚刚..."];
                //                NSLog(@"timeString = %@",timeString);
            }else{
                timeString = [NSString stringWithFormat:@"%@分钟前", timeString];
                //                NSLog(@"timeString = %@",timeString);
            }
         }
          //86400=1天
          if (cha / 3600 > 1 && cha / 86400 < 1){
              timeString = [NSString stringWithFormat:@"%f", cha/3600];
            timeString = [timeString substringToIndex:timeString.length-7];
            timeString=[NSString stringWithFormat:@"%@小时前", timeString];
            //            NSLog(@"timeString = %@",timeString);
          }
          if (cha/86400>1){
              timeString = [NSString stringWithFormat:@"%f", cha/86400];
            timeString = [timeString substringToIndex:timeString.length-7];
            int num=[timeString intValue];
            if (num<2){
                timeString = [NSString stringWithFormat:@"昨天"];
                //                NSLog(@"timeString = %@",timeString);
            }else {
                  NSString * secondStr  = [dateStr substringFromIndex:5];
                timeString = [secondStr substringToIndex:11];
            }
          }
        return timeString;
    }
    
  • 时间戳转化为时间NSDate
    - (NSString *)timeWithTimeIntervalString:(NSString )timeString
    {
    // 格式化时间
    NSDateFormatter
    formatter =[[NSDateFormatter alloc] init];
    formatter.timeZone = [NSTimeZone timeZoneWithName:@"beijing"];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

        // 毫秒值转化为秒
        NSDate* date = [NSDate dateWithTimeIntervalSince1970:[timeString doubleValue]/ 1000.0];
        NSString* dateString = [formatter stringFromDate:date];
        return dateString;
    }
    
  • 将时间戳转化成具体时间字符串(具体到秒)
    - (NSString *)transformTimeStampToTimeStr:(NSString *)timeStamp {
    NSString *time = [NSString stringWithFormat:@"%f",[timeStamp doubleValue]];

        NSInteger num = [time integerValue]/1000;
    
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
        [formatter setDateStyle:NSDateFormatterMediumStyle];
    
        [formatter setTimeStyle:NSDateFormatterShortStyle];
    
        [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    
        NSDate * confromTimesp = [NSDate dateWithTimeIntervalSince1970:num];
        NSString * confromTimespStr = [formatter stringFromDate:confromTimesp];
        return confromTimespStr;
    }

你可能感兴趣的:(iOS 与时间相关的处理)