【问题标题】:How to check the date cross the sunday and rest data如何查看跨星期日和休息数据的日期
【发布时间】:2017-01-02 11:16:39
【问题描述】:

如何查看越过星期日的日期并重置数据?

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

如果用户忘记在休息日打开应用程序,则每个星期日或超过星期日一次需要进行重置。

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

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

【问题讨论】:

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

标签: ios objective-c nsdate


【解决方案1】:

首先,您需要在用户第一次打开您的应用时保存今天的日期和 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 是用户打开您的应用程序的下一个日期,因此您可以使用相同的 dateformater 从[NSDate date] 获取它。 我已经在我的机器上运行了这段代码,它的打印星期天已经过去了,所以重新加载数据所以希望这会对你有所帮助。

【讨论】:

    【解决方案2】:

    和你一样,我们有流量,每个星期五和超过 7 天,它需要更新服务器。 我们用下面的代码做到了: 您需要更新上次更新日期。 看看这个:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-08
      • 2021-08-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多