1. 程式人生 > >根據時間戳返回時間間隔

根據時間戳返回時間間隔

+ (NSString *)getDatePlaySeconds:(NSString *)playSeconds{

NSString *timeStr;

NSTimeInterval time=[playSeconds doubleValue]+28800;

NSDate *detaildate=[NSDatedateWithTimeIntervalSince1970:time];

//當前時間

NSDate *newDate = [NSDatedate];

NSTimeZone *zone = [NSTimeZonesystemTimeZone];

NSTimeInterval timeSystem = [zone

secondsFromGMTForDate:newDate];

NSDate *nowDate = [newDate dateByAddingTimeInterval:timeSystem];

//獲取時間間隔

NSTimeInterval timeCount = [nowDate timeIntervalSinceDate:detaildate];

if (timeCount < 60) {

        timeStr = @"剛剛";

    } elseif (timeCount >= 60 && timeCount < 3600) {

       timeStr = [

NSStringstringWithFormat:@"%d分鐘前", (int)timeCount / 60];

    } elseif(timeCount > 3600 && timeCount < 3600 * 24) {

        timeStr = [NSStringstringWithFormat:@"%d小時前", (int)timeCount / 3600];

    }else{

NSString *str = [NSStringstringWithFormat:@"%@", detaildate];

       timeStr = [str substringWithRange

:NSMakeRange(5, 5)];

    }

return timeStr;

}