根据时间戳转相应的格式-(几秒前,几分钟前,几小时前)

这些是我在项目中常用到的的一些工具栏类方法,希望对大家有用,至今还是菜鸟一枚,如有错误,请及时指正。

1.获取当前手机时间
+ (NSString *)getCurrentDate{
    // 获得时间对象
    NSDate *date = [NSDate date]; 
    //初始化日期格式对象
    NSDateFormatter *forMatter = [[NSDateFormatter alloc] init];
    //格式化时间
    [forMatter setDateFormat:@"yyyyMMdd"];
    //将日期转换为字符串
    NSString *dateStr = [forMatter stringFromDate:date];
    return dateStr;
 }

//后台哥哥改了数据格式,改成了字符串格式,又得改,(2016年12月12日15:03:02修改)
//获取系统的当前时区时间
-(NSDate *)getCurrentLocaleDate:(NSDate *)currenDate {
    //设置时区
    NSTimeZone *timeZone = [NSTimeZone systemTimeZone];
    //目标日期与当前时区的偏移量
    NSTimeInterval interval = [timeZone secondsFromGMTForDate:currenDate];
    //以当前时区时间为标准,加上若干秒后的时间,得到当前系统准确的时间
    NSDate *nowDate = [currenDate dateByAddingTimeInterval:interval];
    return nowDate;
}
//这里我定义成了对象方法,要想公用的话,就用类方法吧~
2.根据传进来的时间戳转换为相应的格式(几秒前,几分钟前,几小时前,几天前)
+ (NSString *)getTimeDifferentWithNowWithTime:(NSString *)createTime{
    //安全校验字符串(后台传进来的时间戳是13位)
    if ([createTime isKindOfClass:[NSNumber class]]) {
        createTime = [NSString stringWithFormat:@"%lld",[createTime longLongValue]];
    }
    
    // 获取当前时间戳
    NSTimeInterval currentTime = [[NSDate date] timeIntervalSince1970];
    //截取字符串
    NSUInteger tt = [[createTime substringToIndex:createTime.length - 3] integerValue];
    //NSUInteger tt = [[createTime longLongValue]/1000];此种写法也是可以的
    // 时间差
    NSTimeInterval time = (currentTime - tt);
    //秒
    NSInteger seconds = time;
    if (seconds < 60) {
        if (seconds <= 0) {
            //有时候会有时间的误差
            seconds = 1;
        }
        return [NSString stringWithFormat:@"%ld%@",seconds,Localized(@"秒前")];
    }
    //秒转分钟
    NSInteger minute = time/60;
    if (minute < 60) {
        return [NSString stringWithFormat:@"%ld%@",minute,Localized(@"分钟前")];
    }
    
    // 秒转小时
    NSInteger hours = time/3600;
    if (hours<24) {
        return [NSString stringWithFormat:@"%ld%@",hours,Localized(@"小时前")];
    }
    //秒转天数
    NSInteger days = time/3600/24;
    if (days < 30) {
        return [NSString stringWithFormat:@"%ld%@",days,Localized(@"天前")];
    }
    //格式化日期对象格式
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];           
    [formatter setDateFormat:@"yy-MM-dd"];
    //将要返回的字符串
    NSString *confromTimespStr = [formatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:tt]];
    return confromTimespStr;
}
3.将字符串转换为字符串"yy-MM-dd"格式
+ (NSString *)getyyyyMMddDateWithTime:(NSString *)time{
    if (time.length > 3) {
        NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[[time substringToIndex:time.length - 3] integerValue]];
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yy-MM-dd"];
        NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
        return confromTimespStr;
    }else{
        return @"";
    } 
}
4.将字符串转为字符串"yy-MM-dd HH:mm:ss"格式
+ (NSString *)getyyyyMMddDateWithTime:(NSString *)time{
    if (time.length > 3) {
        NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[[time substringToIndex:time.length - 3] integerValue]];
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yy-MM-dd"];
        NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
       return confromTimespStr;
    }else{
        return @"";
    }
}
5.将传入的时间戳转换为"yy-MM-dd HH:mm:ss"格式
+ (NSString *)getShouldShowTimeWithBeiJingTime:(NSString *)time{
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[[time substringToIndex:time.length - 3] integerValue]];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yy-MM-dd HH:mm:ss"];
    NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
    return confromTimespStr;   
}
6.将传入的字符串转为日期格式(2016年12月12日15:36:59)
//设置一个未来时间
-(NSDate *)resetTimerWithDate:(NSString *)futureDate{
    //日期格式化
    NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
    //设置当前时区格式
    [dateFormatter setTimeZone:timeZone];
    //此处一定要与字符串格式一致,不然会转换失败 return nil
    if (futureDate.length == 10) {
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    }else{
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    }
    //将字符串转为日期
    NSDate *endDate = [dateFormatter dateFromString:futureDate];
    return endDate;
}

你可能感兴趣的:(根据时间戳转相应的格式-(几秒前,几分钟前,几小时前))