美文网首页
计算时间间隔

计算时间间隔

作者: 化二缺 | 来源:发表于2018-11-24 17:12 被阅读7次
- (NSString *)transformTimeMaker:(NSString *)timestring
{
    NSString *returnString;
    NSDate *timeDate = [self timeFormattTansform:timestring]; //时间戳变成字符串传入
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate:timeDate];
    NSDate *nowDate = [[NSDate date] dateByAddingTimeInterval:interval];
    //两个时间间隔
    NSTimeInterval timeInterval = [timeDate timeIntervalSinceDate:nowDate];

    timeInterval = -timeInterval;
    long temp = 0;
    if (timeInterval < 60) {
        returnString = [NSString stringWithFormat:@"刚刚"];
    } else if ((temp = timeInterval / 60) < 60) {
        returnString = [NSString stringWithFormat:@"%ld分钟前", temp];
    } else if ((temp = timeInterval / (60 * 60)) < 24) {
        returnString = [NSString stringWithFormat:@"%ld小时前", temp];
    } else if ((temp = timeInterval / (24 * 60 * 60)) < 30) {
        returnString = [NSString stringWithFormat:@"%ld天前", temp];
    } else if (((temp = timeInterval / (24 * 60 * 60 * 30))) < 12) {
        returnString = [NSString stringWithFormat:@"%ld月前", temp];
    } else {
        temp = timeInterval / (24 * 60 * 60 * 30 * 12);
        returnString = [NSString stringWithFormat:@"%ld年前", temp];
    }
    return returnString;
}

- (NSDate *)timeFormattTansform:(NSString *)timeString
{
    NSString *timeStampString = timeString;
    NSTimeInterval interval = [timeStampString doubleValue];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval];
    return date;
}

相关文章

网友评论

      本文标题:计算时间间隔

      本文链接:https://www.haomeiwen.com/subject/iysyqqtx.html