判断是否为今年、是否为今天、是否为昨天,比较当前时间和from时间的差值

来源:互联网 发布:淘宝恒源祥是正品吗 编辑:程序博客网 时间:2024/05/18 03:16

效果图:

时间处理样式:

1:创建NSDate的扩展类

  NSDate+LMExtension.h

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. //  Created by limin on 16/6/21.  
  2. //  Copyright © 2016年 limin. All rights reserved.  
  3. //  
  4.   
  5. #import <Foundation/Foundation.h>  
  6.   
  7. @interface NSDate (LMExtension)  
  8. /** 比较当前时间和from时间的差值*/  
  9. -(NSDateComponents *)deltaFrom:(NSDate *)from;  
  10. /** 是否为今年*/  
  11. -(BOOL)isThisYear;  
  12. /** 是否为今天*/  
  13. -(BOOL)isToday;  
  14. /** 是否为昨天*/  
  15. -(BOOL)isYesterday;  
  16. @end  

NSDate+LMExtension.m

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. //  
  2. //  Created by limin on 16/6/21.  
  3. //  Copyright © 2016年 limin. All rights reserved.  
  4. //  
  5.   
  6. #import "NSDate+LMExtension.h"  
  7.   
  8. @implementation NSDate (LMExtension)  
  9. -(NSDateComponents *)deltaFrom:(NSDate *)from  
  10. {  
  11.     //日历  
  12.     NSCalendar *calendar = [NSCalendar currentCalendar];  
  13.     //比较时间  
  14.     NSDateComponents *comps = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond fromDate:from toDate:self options:0];  
  15.     return comps;  
  16. }  
  17. -(BOOL)isThisYear  
  18. {  
  19.     NSDate *now = [NSDate date];  
  20.     //日历  
  21.     NSCalendar *calendar = [NSCalendar currentCalendar];  
  22.     NSInteger nowYear = [calendar component:NSCalendarUnitYear fromDate:now];  
  23.     NSInteger selfYear = [calendar component:NSCalendarUnitYear fromDate:self];  
  24.       
  25.     return nowYear == selfYear;  
  26.       
  27. }  
  28. /** 方法1:判断是否为今天*/  
  29. -(BOOL)isToday  
  30. {  
  31.     //日历  
  32.     NSCalendar *calendar = [NSCalendar currentCalendar];  
  33.     NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;  
  34.       
  35.     NSDateComponents *nowComps = [calendar components:unit fromDate:[NSDate date]];  
  36.     NSDateComponents *selfComps = [calendar components:unit fromDate:self];  
  37.       
  38.     return nowComps.year == selfComps.year && nowComps.month == selfComps.month && nowComps.day == selfComps.day;  
  39. }  
  40. /** 方法2:判断是否为今天*/  
  41. //-(BOOL)isToday  
  42. //{  
  43. //    NSDateFormatter *dateformatter = [[NSDateFormatter alloc]init];  
  44. //    dateformatter.dateFormat = @"yyyy-MM-dd";  
  45. //      
  46. //    NSString *nowString = [dateformatter stringFromDate:[NSDate date]];  
  47. //    NSString *selfString = [dateformatter stringFromDate:self];  
  48. //    return [nowString isEqualToString:selfString];  
  49. //}  
  50. /** 判断是否为昨天*/  
  51. -(BOOL)isYesterday  
  52. {  
  53.     NSDateFormatter *dateformatter = [[NSDateFormatter alloc]init];  
  54.     dateformatter.dateFormat = @"yyyy-MM-dd";  
  55.     NSDate *nowDate = [dateformatter dateFromString:[dateformatter stringFromDate:[NSDate date]]];  
  56.     NSDate *selfDate = [dateformatter dateFromString:[dateformatter stringFromDate:self]];  
  57.       
  58.     NSCalendar *calendar = [NSCalendar currentCalendar];  
  59.     NSDateComponents *comps = [calendar components: NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:selfDate toDate:nowDate options:0];  
  60.     return comps.year == 0 && comps.month == 0 && comps.day == 1;  
  61. }  
  62.   
  63. @end  
原文地址链接:http://blog.csdn.net/leemin_ios/article/details/51881853    点击打开链接
2: 时间赋值.

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. //Label赋值创建时间  
  2. [self setupCreateTime:topicsModel.create_time];  

setupCreateTime: 方法实现

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #pragma mark - 时间处理  
  2. /** 
  3.  *self.createTimeLabel:时间标签 
  4.  *今天: 
  5.  [ 
  6.  1分钟内:刚刚 
  7.  1小时内:xx分钟前 
  8.  其他:xx小时前 
  9.  ] 
  10.  *昨天:几点几分 
  11.  *其他:6-13 19:12 
  12.  *不是今年:去年:年份2014-5-8 18:40:30 
  13.  */  
  14. -(void)setupCreateTime:(NSString *)create_time  
  15. {  
  16.       
  17.     //设置日期格式  
  18.     NSDateFormatter *formatter = [[NSDateFormatter alloc]init];  
  19.     formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";  
  20.     //创建时间  
  21.     NSDate *createDate = [formatter dateFromString:create_time];  
  22.       
  23.       
  24.     if (createDate.isThisYear) {//今年  
  25.           
  26.         if (createDate.isToday) {//今天  
  27.               
  28.             //当前时间  
  29.             NSDate *nowDate = [NSDate date];  
  30.             //比较时间  
  31.             NSDateComponents *comps = [nowDate deltaFrom:createDate];  
  32.             if (comps.hour >= 1) {  
  33.                 //时间差距>=1小时  
  34.                 [self.createTimeLabel setText:[NSString stringWithFormat:@"%zd小时前",comps.hour]];  
  35.             }else if (comps.minute >= 1)  
  36.             {  
  37.                 //时间差距<1小时,>=1分钟  
  38.                [self.createTimeLabel setText:[NSString stringWithFormat:@"%zd分钟前",comps.minute]];  
  39.             }else  
  40.             {// 时间差距<1分钟  
  41.                 [self.createTimeLabel setText:@"刚刚"];  
  42.             }  
  43.               
  44.               
  45.         }else if (createDate.isYesterday)//昨天  
  46.         {  
  47.             formatter.dateFormat = @"昨天 HH:mm:ss";  
  48.             [self.createTimeLabel setText:[formatter stringFromDate:createDate]];  
  49.         }else  
  50.         {//其他  
  51.             formatter.dateFormat = @"MM-dd HH:mm:ss";  
  52.             [self.createTimeLabel setText:[formatter stringFromDate:createDate]];  
  53.         }  
  54.     }else//非今年  
  55.     {  
  56.         [self.createTimeLabel setText:create_time];  
  57.     }  
  58.       
  59. }  
0 0