2017-01-02 62 views
0

如何查看周日的日期并重置数据?如何查看星期日和休息日的数据

我需要在每个星期日进行重置,如果我在星期天错过了,那么如果用户打开应用程序的下一个递增日期(即星期一,星期二,直到星期六),需要重置。

如果用户忘记在休息日期打开应用程序,则对于每个星期日或星期日穿越一次重置都需要发生。

继我的代码,我尝试实施。 哪一个没有按照我的要求工作,您的反馈非常感谢。

 -(void) weekDaySundayToRefresh 
     { 
      NSDate *now = [NSDate date]; 
      NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; 
      NSDateComponents *components = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitWeekOfMonth | NSCalendarUnitWeekday fromDate:now]; 

      NSUInteger weekdayToday = [components weekday]; 
      NSInteger daysToMonday = (8 - weekdayToday) % 7; 

      nextSunday= [now dateByAddingTimeInterval:60*60*24*daysToSunday]; 

      NSLog(@"nextMonday : %@", nextSunday); 
     } 

     -(void) compareDate 
     { 
      NSDate *today = [NSDate date]; // it will give you current date 
      // NSDate *newDate = [MH_USERDEFAULTS objectForKey:@"USER_BACKGROUND_DATE"]; // your date 

      NSComparisonResult result; 
      //has three possible values: NSOrderedSame,NSOrderedDescending, NSOrderedAscending 

      result = [today compare:refreshGoalsDate]; // comparing two dates 

      if(result==NSOrderedAscending) 
      { 
       NSLog(@"today is less"); 
      } 
      else if(result==NSOrderedDescending) 
      { 
       NSLog(@"newDate is less"); 

       NSDate *fromDate; 
       NSDate *toDate; 

       NSCalendar *calendar = [NSCalendar currentCalendar]; 

       [calendar rangeOfUnit:NSCalendarUnitDay startDate:&fromDate 
          interval:NULL forDate:refreshGoalsDate]; 
       [calendar rangeOfUnit:NSCalendarUnitDay startDate:&toDate 
          interval:NULL forDate:today]; 

       NSDateComponents *difference = [calendar components:NSCalendarUnitDay 
                  fromDate:fromDate toDate:toDate options:0]; 

       noOfDaysCount = [difference day]; 
       NSLog(@"date difference:%ld",(long)[difference day]); 
      } 
      else 
       NSLog(@"Both dates are same"); 
     } 

    -(void) checkRefreshGoals:(int)sectionIndex rowIndex:(int)rowIndex 
    { 
     NSDateFormatter * formatter = [NSDateFormatter new]; 
     [formatter setDateFormat:@"yyyy-MM-dd"]; 
     NSString * currentDateString = [formatter stringFromDate:[NSDate date]]; 
     NSString * initialNotificationDate = [formatter stringFromDate:refreshDate]; 
     NSDate *currentDate = [formatter dateFromString:currentDateString]; 
     NSDate *refreshDate = [formatter dateFromString:initialNotificationDate]; 

     if ([refreshDate compare:currentDate] == NSOrderedDescending) 
     { 
      NSLog(@"date1 is later than date2"); 
      isGoalsRefreshed = NO; 
     } 
     else if ([refreshDate compare:currentDate] == NSOrderedAscending) 
     { 
      NSLog(@"date1 is earlier than date2"); 
      if (sameDay == YES) 
      { 
       isGoalsRefreshed = YES; 
       [self refreshDataOnSunday:sectionIndex rowIndex:rowIndex]; 
      } 
      else if (noOfDaysCount > 7) 
      { 
       isGoalsRefreshed = YES; 
       [self refreshDataOnSunday:sectionIndex rowIndex:rowIndex]; 
      } 
      else 
      { 
       isGoalsRefreshed = NO; 
      } 
     } 
     else 
     { 
      NSLog(@"dates are the same"); 
      isRefreshed = NO; 
     } 
    } 

-(void) refreshDataOnSunday:(int)sectionIndex rowIndex:(int)rowIndex 
{ 
    if (noOfDaysCount > 7) 
    { 
     if (isGoalsRefreshed == YES) 
     { 

      // Do rest your data 


     } 
    } 
    else if (sameDay == YES) 
    { 
     if (isGoalsRefreshed == YES) 
     { 
     // Do reset your data 

     } 
    } 
    else 
    { 
     isGoalsRefreshed = NO; 
    } 
} 
+0

你会存储你上次更新的时间吗? – karthikeyan

回答

0

首先,你需要的时候,在用户第一次打开你的应用程序,那么你就需要比较下一个应用开放日期,以便您可以估算或者周日消失或不保存今天的日期和DayNumber。

/*first check in userDefault for stored Date and Day 
Store values in local and check with current date */ 

NSMutableDictionary *dictStoredData = [USERDEFAULT valueForKey:@"dateTimeDate"]; 

if (!dictStoredData) { 
    // user comes first time so store it here 
    dictStoredData =[[NSMutableDictionary alloc] init]; 

    NSDate *today =[NSDate date]; 
    NSCalendar *gregorian =[[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; 
    NSDateComponents *comps =[gregorian components:NSCalendarUnitWeekday fromDate:[NSDate date]]; 

    NSInteger weekday = [comps weekday]; 
    int nextSundayVal = 7 - (int)weekday; 

    NSDateFormatter *format = [[NSDateFormatter alloc] init]; 
    [format setDateFormat:@"yyyy-MM-dd"]; 
    NSString *startDate = [format stringFromDate: today]; 
    [dictStoredData setObject:startDate forKey:@"startDate"]; 
    [dictStoredData setObject:[NSString stringWithFormat:@"%d",nextSundayVal] forKey:@"nextSundayVal"]; 

    [USERDEFAULT setObject:dictStoredData forKey:@"dateTimeDate"]; 
} 
else{ 
     NSString *strEndDate = @"2017-01-08"; 
     NSString *strStartDate = [dictStoredData valueForKey:@"startDate"]; 
     int nextSundayVal = (int)[dictStoredData valueForKey:@"nextSundayVal"]; 
     NSDateFormatter *format = [[NSDateFormatter alloc] init]; 
     [format setDateFormat:@"yyyy-MM-dd"]; 
     NSDate *startDate = [format dateFromString:strStartDate]; 
     NSDate *endDate = [format dateFromString:strEndDate]; 

     NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; 
     NSDateComponents *components = [gregorianCalendar components:NSCalendarUnitDay 
                  fromDate:startDate 
                   toDate:endDate 
                  options:0]; 

     int newDayCount = (int)[components day]; 

     if (newDayCount > nextSundayVal) { 
      NSLog(@"sunday is gone so reload data!!"); 
      //save this date as a new date and check with this when user come again 
     } 
     else { 
      NSLog(@"sunday is not come yet!!"); 
     } 

} 

这里strEndDate是当用户打开应用程序,以便您可以从[NSDate date]与同一dateformater得到它的下一个日期。 我已经在我的机器上运行此代码,并且它的打印星期日不见了,所以请重新加载数据,所以希望这会对您有所帮助。

0

就像你我们有流量,每个星期五和七天以上,它需要更新服务器。 我们用以下代码完成: 您需要更新上次更新的日期。 查看:

 if ([isFirday isEqualToString:@"Friday"]) {//This is my value from server side (they will give as friay), i think you dont need this condition. 
      BOOL isSameDay; 
      NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
      [dateFormatter setDateFormat:@"MMM dd yyyy,HH:mm"]; 
      NSDate *date = [dateFormatter dateFromString:mennuRefreshTime];//Is an last updated date. 
      [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 
      NSString *temp = [dateFormatter stringFromDate:date]; 
      NSDate *lastDate = [dateFormatter dateFromString:temp]; 

      NSString *currentDaystr = [dateFormatter stringFromDate:[NSDate date]]; 
      // Write the date back out using the same format 
      NSDate *currentDate = [dateFormatter dateFromString:currentDaystr]; 

      if ([lastDate compare:currentDate] == NSOrderedDescending) { 
       NSLog(@"date1 is later than date2"); 
       isSameDay = NO; 
      } else if ([lastDate compare:currentDate] == NSOrderedAscending) { 
       NSLog(@"date1 is earlier than date2"); 
       [dateFormatter setDateFormat:@"EEEE"]; 
       NSString *dayName = [dateFormatter stringFromDate:currentDate]; 
       NSString *lastupdateName = [dateFormatter stringFromDate:lastDate]; 

       if ([dayName isEqualToString:lastupdateName]) { 
        isSameDay = YES; 
       }else{ 
        isSameDay = NO; 
       } 

      } else { 
       NSLog(@"dates are the same"); 
       isSameDay = YES; 
      } 
      if (isSameDay == YES) { 
       return; 
      } 


      NSCalendar *calender =[[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; 
      NSDateComponents *components = [calender components:NSCalendarUnitDay fromDate:lastDate toDate:currentDate options:0]; 

      int day = (int)[components day]; 
      if (day >= 7) { 
       [self getTopList]; 
      }else{ 
       [dateFormatter setDateFormat:@"EEEE"]; 
       NSString *dayName = [dateFormatter stringFromDate:currentDate]; 
       if ([dayName isEqualToString:isFirday]) { 
        [self getTopList]; 
       }else{ 
        return; 
       } 
      } 


     }