IOS 判断某个时间距离现在已经过了多久

+(NSString *)getUTCFormateDate:(NSString *)newsDate

{

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

NSLog(@"newsDate = %@",newsDate);

NSDate *newsDateFormatted = [dateFormatter dateFromString:newsDate];

NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];

[dateFormatter setTimeZone:timeZone];

NSDate* current_date = [[NSDate alloc] init];

NSTimeInterval time=[current_date timeIntervalSinceDate:newsDateFormatted];//间隔的秒数

int year =((int)time)/(3600*24*30*12);

int month=((int)time)/(3600*24*30);

int days=((int)time)/(3600*24);

int hours=((int)time)%(3600*24)/3600;

int minute=((int)time)%(3600*24)/60;

NSLog(@"time=%f",(double)time);

NSString *dateContent;

if (year!=0) {

dateContent = newsDate;

}else if(month!=0){

dateContent = [NSString stringWithFormat:@"%@%i%@",@"  ",month,@"个月前"];

}else if(days!=0){

dateContent = [NSString stringWithFormat:@"%@%i%@",@"  ",days,@"天前"];

}else if(hours!=0){

dateContent = [NSString stringWithFormat:@"%@%i%@",@"  ",hours,@"小时前"];

}else {

dateContent = [NSString stringWithFormat:@"%@%i%@",@"  ",minute,@"分钟前"];

}

return dateContent;

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 最近想整理下关于OC的工具类,常用到的也方便自己之后查阅,也可以给需要的朋友参考。 1、两种倒计时 第一种:GCD...
    SPIREJ阅读 1,454评论 3 4
  • .h文件 #import #define D_MINUTE60 #define D_HOUR3600 #defin...
    屈涯阅读 707评论 2 0
  • 自己用过,写过的一点分类,记录一下先。 NSdate .h中: // 是否为今天 - (BOOL)isToday;...
    水煮杰尼龟阅读 623评论 0 0
  • 1.生日正则表达式 #pragma 正则匹配生日 + (BOOL)checkBirthNumber:(NSStri...
    肉肉要次肉阅读 3,017评论 1 1
  • 蒹葭苍苍,白露为霜。 当阳光下自己的身影越来越短时,才蓦然发现酷热的夏天已然悄悄远去了,正午树上知了的鸣叫也仿佛慵...
    ALuckyMe阅读 269评论 0 0

友情链接更多精彩内容