说说发送消息时间确定(时间戳与时间之间的转换)

使用说明,在这里是定义一个类继承于NSObject即可,调用下面的方法即可

 #import "CIOTimer.h"

 @implementation CIOTimer
  /**
   * 计算指定时间与当前的时间差
   * @param compareDate   某一指定时间
   * @return 多少(秒or分or天or月or年)+前 (比如,3天前、10分钟前)
   */
   +(NSString *)compareCurrentTime:(NSDate*) compareDate
   //
   {
     NSTimeInterval  timeInterval = [compareDate timeIntervalSinceNow];
     timeInterval = -timeInterval;

     NSInteger time = round(timeInterval);

     long temp = 0;

     if (time < 60) {
    
      NSString *result = @"刚刚";
    
      return result;
     }
     else if((temp = timeInterval/60) <60){
     NSString *result = [NSString stringWithFormat:@"%ld分前",temp];
     return result;
    }

    else if((temp = temp/60) <24){
    NSString *result = [NSString stringWithFormat:@"%ld小前",temp];
    return result;
   }

     else if((temp = temp/24) <30){
     NSString *result = [NSString stringWithFormat:@"%ld天前",temp];
     return result;
     }

    else if((temp = temp/30) <12){
    NSString *result = [NSString stringWithFormat:@"%ld月前",temp];
    return result;
    }
    else{
    temp = temp/12;
    
    NSString *result = [NSString stringWithFormat:@"%ld年前",temp];
    return result;
    }

    return  nil;
  }
  @end

具体的使用方法如下

1.导入类名 #import "CIOTimer.h"
2. 导入后台返回的时间戳

NSDate *datedate = [NSDate dateWithTimeIntervalSince1970:[@"1363948516" floatValue]];
NSString *string1 = [CIOTimer compareCurrentTime: datedate];

NSLog(@"返回=%@",string1);

具体的代码 密码: ycid

补充:时间戳转具体的时间

 NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[@"1363948516" floatValue]];

 - (NSString *)getBabyDetailAge:(NSDate *)date
 {
    //    NSTimeInterval time=[date doubleValue];
    //    NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];

    //NSLog(@"date:%@",[detaildate description]);

    NSDateFormatter *formatter_ = [[NSDateFormatter alloc] init];

     [formatter_ setDateFormat:@"MM-dd HH:mm"];

     NSString *dateString = [formatter_  stringFromDate:date];
     dateString = [dateString stringByReplacingOccurrencesOfString:@"-" withString:@"/"];

     return dateString;
  }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容