如何检查日期和周日和休息数据

时间:2017-01-02 11:16:40

标签: ios objective-c nsdate

如何检查星期日的日期并重置数据?

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

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

按照我尝试实施的代码。 根据我的要求,这不符合您的要求,我们非常感谢您的反馈。

        -(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;
    }
}

2 个答案:

答案 0 :(得分: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]获取该应用程序。 我已经在我的机器中运行了这段代码,并且它的打印星期日已经消失,所以重新加载数据,所以希望这会对你有所帮助。

答案 1 :(得分: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;
                }
            }


        }