时间筛选之 本周、上周、更早

转载自:  http://blog.csdn.net/w250130255/article/details/48000895

1、可以实现时间筛选,目测应该还有更简单的方法。

思路: 获取当天时间,和目标时间进行比对操作。

注意点:时区 时区 时区 重要的时区说三遍。

[objc]  view plain  copy
  1. - (void)viewDidLoad {  
  2.     [super viewDidLoad];  
  3.     NSDate * today = [NSDate date];  
  4.       
  5.     //定义当前时区时间  
  6.     NSString * time =@"2015-08-16 00:00:00";  
  7.     NSDate* xmlData = [self dateFromString:time];  
  8.       
  9.     xmlData = [self getNowDateFromatAnDate:xmlData];  
  10.       
  11.     if([self intervalSinceNow:xmlData]>=[self getTodayisWeek:today]+7)  
  12.     {  
  13.         NSLog(@"这个日期是很早以前");  
  14.     }  
  15.     if([self intervalSinceNow:xmlData]>=[self getTodayisWeek:today]&&[self intervalSinceNow:xmlData]<[self getTodayisWeek:today]+7)  
  16.     {  
  17.         NSLog(@"这个日期是上周");  
  18.     }  
  19.     if([self intervalSinceNow:xmlData]<7&&[self intervalSinceNow:xmlData]<[self getTodayisWeek:today])  
  20.     {  
  21.         NSLog(@"这个日期是本周");  
  22.     }  
  23.       
  24.       
  25. }  
  26.   
  27. - (int)intervalSinceNow: (NSDate *) theDate  
  28. {  
  29.     NSTimeInterval late=[theDate timeIntervalSince1970]*1;  
  30.       
  31.     NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];  
  32.     dat=  [self getNowDateFromatAnDate:dat];  
  33.     NSTimeInterval now=[dat timeIntervalSince1970]*1;  
  34.     NSString *timeString=@"";  
  35.       
  36.     NSTimeInterval cha=now-late;  
  37.       
  38.     if (cha/86400>1)  
  39.     {  
  40.         timeString = [NSString stringWithFormat:@"%f", cha/86400];  
  41.         timeString = [timeString substringToIndex:timeString.length-7];  
  42.         return [timeString intValue];  
  43.     }  
  44.     return -1;  
  45.      
  46. }  
  47.   
  48. - (NSDate *)getNowDateFromatAnDate:(NSDate *)anyDate  
  49. {  
  50.     //设置源日期时区  
  51.     NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];//或GMT  
  52.     //设置转换后的目标日期时区  
  53.     NSTimeZone* destinationTimeZone = [NSTimeZone localTimeZone];  
  54.     //得到源日期与世界标准时间的偏移量  
  55.     NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:anyDate];  
  56.     //目标日期与本地时区的偏移量  
  57.     NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:anyDate];  
  58.     //得到时间偏移量的差值  
  59.     NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;  
  60.     //转为现在时间  
  61.     NSDate* destinationDateNow = [[NSDate alloc] initWithTimeInterval:interval sinceDate:anyDate];  
  62.     return destinationDateNow;  
  63. }  
  64. - (NSDate *)dateFromString:(NSString *)dateString{  
  65.       
  66.     NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];  
  67.       
  68.     [dateFormatter setDateFormat@"yyyy-MM-dd HH:mm:ss"];  
  69.       
  70.      NSDate *destDate= [dateFormatter dateFromString:dateString];  
  71.       
  72.      return destDate;  
  73.       
  74. }  
  75.   
  76. //获得某日是周几  
  77. -(int)getTodayisWeek:(NSDate *)today  
  78. {  
  79.     today = [self getNowDateFromatAnDate:today];  
  80.     NSCalendar*calendar = [NSCalendar currentCalendar];  
  81.     NSDateComponents*comps;  
  82.     [comps setWeekday:0];  
  83.     comps =[calendar components:(NSCalendarUnitWeekday)fromDate:today];  
  84.     NSInteger weekday = [comps weekday]; // 星期几(注意,周日是“1”,周一是“2”。。。。)  
  85.     int  factWeekDay;  
  86.     //默认是周日开始  
  87.     switch (weekday) {  
  88.         case 1:  
  89.             factWeekDay = 7  ;  
  90.             break;  
  91.         case 2:  
  92.             factWeekDay = 1  ;  
  93.             break;  
  94.         case 3:  
  95.             factWeekDay = 2  ;  
  96.             break;  
  97.         case 4:  
  98.             factWeekDay = 3  ;  
  99.             break;  
  100.         case 5:  
  101.             factWeekDay = 4  ;  
  102.             break;  
  103.         case 6:  
  104.             factWeekDay = 5  ;  
  105.             break;  
  106.         case 7:  
  107.             factWeekDay = 6  ;  
  108.             break;  
  109.         default:  
  110.             break;  
  111.     }  
  112.       
  113.     return  factWeekDay;  
  114. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值