OC Extension Time(时间)

一直觉得自己写的不是技术,而是情怀,一个个的教程是自己这一路走来的痕迹。靠专业技能的成功是最具可复制性的,希望我的这条路能让你们少走弯路,希望我能帮你们抹去知识的蒙尘,希望我能帮你们理清知识的脉络,希望未来技术之巅上有你们也有我。

OC 项目底层框架(万能)

Time(时间)下载链接

注意:

下面两个方法下载Time文件没有的,在OC 项目底层框架(万能)有。

//获取距离现在过了多少时间(跟微信一样)(好用)
+ (NSString *)getCurrrentTimeToReleaseTime:(NSString *)releaseTime;

//获取距离现在过了多少时间(一般)
+ (NSString *)getDistanceTimeWithBeforeTime:(NSString *)beTime;

在这里插入图片描述
.h

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface Time : NSString

/**
 *  获取当前的时间
 *
 *  @param type  时间格式(YYYY-MM-dd hh:mm:ss SSS)  (YYYY-MM-dd)  (MM-dd) (hh:mm:ss SSS) (hhmmssSSS)
 *
 *  @return  时间字符串
 */
+(NSString*)getCurrentTimesWithType:(NSString *)type;

//获取当前时间的时间戳(秒)  方法1
+(NSString *)getNowTimeTimestampaSecondA;

//获取当前时间的时间戳(秒)  方法2
+(NSString *)getNowTimeTimestampSecondB;

//获取当前时间戳  (以毫秒为单位)
+(NSString *)getNowTimeTimestampMillisecond;

//获取距离现在过了多少时间(跟微信一样)(好用)
+ (NSString *)getCurrrentTimeToReleaseTime:(NSString *)releaseTime;

//获取距离现在过了多少时间(跟微信一样)(一般)
+ (NSString *)getDistanceTimeWithBeforeTime:(NSString *)beTime;
@end

.m

/**
 *  获取当前的时间
 *
 *  @param type  时间格式(YYYY-MM-dd hh:mm:ss SSS)  (YYYY-MM-dd)  (MM-dd) (hh:mm:ss SSS) (hhmmssSSS)
 *
 *  @return  时间字符串
 */
+(NSString*)getCurrentTimesWithType:(NSString *)type{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:type];
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    [formatter setTimeZone:timeZone];
    NSDate *datenow = [NSDate date];//现在时间,你可以输出来看下是什么格式
    return [formatter stringFromDate:datenow];
}

//获取当前时间戳有两种方法(以秒为单位)
//获取当前时间的时间戳(秒)  方法1
+(NSString *)getNowTimeTimestampaSecondA{
    return [NSString stringWithFormat:@"%ld", (long)[[NSDate date] timeIntervalSince1970]];
}

//获取当前时间的时间戳(秒)  方法2
+(NSString *)getNowTimeTimestampSecondB{
  NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
  NSTimeInterval a=[dat timeIntervalSince1970];
  NSString*timeString = [NSString stringWithFormat:@"%0.f", a];//转为字符型
  return timeString;
}

//获取当前时间戳  (以毫秒为单位)
+(NSString *)getNowTimeTimestampMillisecond{
  return [NSString stringWithFormat:@"%ld", (long)[[NSDate date] timeIntervalSince1970]*1000];
}

//获取距离现在过了多少时间(跟微信一样)(好用)
+ (NSString *)getCurrrentTimeToReleaseTime:(NSString *)releaseTime{
    
    NSDate * nowDate = [NSDate date];
    
    NSTimeInterval now = [nowDate timeIntervalSince1970];
    NSTimeInterval yet = [releaseTime doubleValue];

    NSTimeInterval newTime = now - yet;

    NSString * mm = [NSString stringWithFormat:@"%.2f",newTime/60];
    NSString * hh = [NSString stringWithFormat:@"%.2f",newTime/60/60];
    NSString * dd = [NSString stringWithFormat:@"%.2f",newTime/60/60/24];
    NSString * MM = [NSString stringWithFormat:@"%.2f",newTime/60/60/24/30];
    
    NSString * date;
    
    if ([MM floatValue] >= 1) {
        date = [NSString stringWithFormat:@"%.f个月前",[MM floatValue]];
    }else if ([dd floatValue] >= 1) {
        date = [NSString stringWithFormat:@"%.f天前",[dd floatValue]];
    }else if ([hh floatValue] >= 1) {
        date = [NSString stringWithFormat:@"%.f小时前",[hh floatValue]];
    }else if ([mm floatValue] >= 1) {
        date = [NSString stringWithFormat:@"%.f分钟前",[mm floatValue]];
    }else {
        date = [NSString stringWithFormat:@"%.f秒前",newTime];
    }
    return date;
}

//获取距离现在过了多少时间(跟微信一样)(一般)
+ (NSString *)getDistanceTimeWithBeforeTime:(NSString *)beTime{
    NSTimeInterval now = [[NSDate date] timeIntervalSince1970];
    double distanceTime = now - [beTime doubleValue];
    NSString * distanceStr;
    NSDate * beDate = [NSDate dateWithTimeIntervalSince1970:[beTime doubleValue]];
    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 <60*60) {//时间小于一个小时
        distanceStr = [NSString stringWithFormat:@"%ld分钟前",(long)distanceTime/60];
    } else if(distanceTime <24*60*60 && [nowDay integerValue] == [lastDay integerValue]){//时间小于一天
        distanceStr = [NSString stringWithFormat:@"今天 %@",timeStr];
    } else if(distanceTime<24*60*60*2 && [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 <24*60*60*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;
}
@end

使用

//获取当前的时间
[Time getCurrentTimesWithType:@"YYYY-MM-dd hh:mm:ss"];  //Time - 2019-06-19 15:04:07

//获取当前时间的时间戳(秒)  方法1
[Time getNowTimeTimestampaSecondA]; //Time - 1560927922

//获取当前时间的时间戳(秒)  方法2
[Time getNowTimeTimestampSecondB]; //Time - 1560927984

//获取当前时间戳  (以毫秒为单位)
[Time getNowTimeTimestampMillisecond]; //Time - 1560927957000

//获取距离现在过了多少时间(跟微信一样)(好用)
NSString *time_0 = [Time getCurrrentTimeToReleaseTime:@"1654080757"];//  2小时前

//获取距离现在过了多少时间(一般)
NSString *time_1 = [Time getDistanceTimeWithBeforeTime:@"1654080757"];//  今天 18:52
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冯汉栩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值