一直觉得自己写的不是技术,而是情怀,一个个的教程是自己这一路走来的痕迹。靠专业技能的成功是最具可复制性的,希望我的这条路能让你们少走弯路,希望我能帮你们抹去知识的蒙尘,希望我能帮你们理清知识的脉络,希望未来技术之巅上有你们也有我。
注意:
下面两个方法下载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