iOS時間格式化“剛剛、幾分鐘前、幾小時前”等,[包括時間戳&格式化後的時間]...

weixin_34138377發表於2016-11-26

前言:
iOS中把時間轉化成“剛剛、幾分鐘前、幾小時前、幾天前、某月某日幾點幾分、.......”格式
這就需要看後臺返回什麼樣的型別了,在專案中碰到的無非就是,格式化後和時間戳了。所以,這次把這兩種我都結合起來,放到這裡,直接拿去用!

一、返回格式化後的時間 2016-10-11 12:33:33

pragma mark 時間格式轉化

注意:資料返回型別為格式化後的時間 eg: "2016-10-11 12:33:33"

  • (NSString *) compareCurrentTime:(NSString *)str
    {

    //把字串轉為NSdate
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *timeDate = [dateFormatter dateFromString:str];

    //得到與當前時間差
    NSTimeInterval timeInterval = [timeDate timeIntervalSinceNow];
    timeInterval = -timeInterval;
    //標準時間和北京時間差8個小時
    // timeInterval = timeInterval - 86060;
    long temp = 0;
    NSString *result;
    if (timeInterval < 60) {
    result = [NSString stringWithFormat:@"剛剛"];
    }
    else if((temp = timeInterval/60) <60){
    result = [NSString stringWithFormat:@"%ld分鐘前",temp];
    }

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

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

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

return  result;

}

二、返回的是時間戳

pragma mark 時間格式轉化

注意:後臺返回的時間戳包括10位或者有小數點。
eg:“1480064761” 1480064761.000000

  • (NSString *)distanceTimeWithBeforeTime:(double)beTime
    {
    NSTimeInterval now = [[NSDate date]timeIntervalSince1970];
    double distanceTime = now - beTime;
    NSString * distanceStr;

    NSDate * beDate = [NSDate dateWithTimeIntervalSince1970:beTime];
    NSDateFormatter * df = [[NSDateFormatter alloc]init];
    [df setDateFormat:@"HH:mm"];
    NSString * timeStr = [df stringFromDate:beDate];

    [df setDateFormat:@"dd"];
    NSString * nowDay = [df stringFromDate:[NSDate date]];
    NSString * lastDay = [df stringFromDate:beDate];

    if (distanceTime < 60) {
    distanceStr = @"剛剛";
    }
    else if (distanceTime <6060) {
    distanceStr = [NSString stringWithFormat:@"%ld分鐘前",(long)distanceTime/60];
    }
    else if(distanceTime <24
    6060 && [nowDay integerValue] == [lastDay integerValue]){
    distanceStr = [NSString stringWithFormat:@"今天 %@",timeStr];
    }
    else if(distanceTime<24
    60602 && [nowDay integerValue] != [lastDay integerValue]){

      if ([nowDay integerValue] - [lastDay integerValue] ==1 || ([lastDay integerValue] - [nowDay integerValue] > 10 && [nowDay integerValue] == 1)) {
          distanceStr = [NSString stringWithFormat:@"昨天 %@",timeStr];
      }
      else{
          [df setDateFormat:@"MM-dd HH:mm"];
          distanceStr = [df stringFromDate:beDate];
      }
    

    }
    else if(distanceTime <246060*365){
    [df setDateFormat:@"MM-dd HH:mm"];
    distanceStr = [df stringFromDate:beDate];
    }
    else{
    [df setDateFormat:@"yyyy-MM-dd HH:mm"];
    distanceStr = [df stringFromDate:beDate];
    }
    return distanceStr;
    }

三、捎帶福利、獲取當前時間戳方法,返回10位。

pragma mark 獲取當前時間戳

  • (NSString *)getCurrentTime{
    NSDate *senddata = [NSDate date];
    NSString *date2 = [NSString stringWithFormat:@"%ld", (long)[senddata timeIntervalSince1970]];
    return date2;
    }

相關文章