gpt4 book ai didi

ios - 检查日期是否在范围内

转载 作者:行者123 更新时间:2023-12-01 17:42:27 25 4
gpt4 key购买 nike

我有一个使用UICollectionView的自定义日历设置,并且有一个可以保存30天的日期的数组。我想做的是找出一个值是否在当前日期和它之前的7天之间。如果是这样,我需要它做某事。我无法弄清楚我在做什么错。这是我到目前为止的内容:

    NSDateFormatter *df = [NSDateFormatter new];
[df setDateFormat:@"MM-dd-yy"];
NSdate *date = [NSDate date];
NSDate *newCurrentDate = [date dateByAddingTimeInterval:+14*24*60*60];
NSDate *sevenDaysBeforeCurrent = [date dateByAddingTimeInterval:-7*24*60*60];




if([[df stringFromDate:[days objectAtIndex:indexPath.item]] compare:[df stringFromDate:newCurrentDate]] == NSOrderedAscending && [[df stringFromDate:[days objectAtIndex:indexPath.item]] compare:[df stringFromDate:sevenDaysBeforeCurrent]] == NSOrderedDescending )
{

UIImage *buttonImage = [UIImage imageNamed:@"dates.png"];
[myButton setBackgroundImage:buttonImage forState:UIControlStateNormal];

}

它的作用是更改newCurrentDate之前的每个日期的图像,而不是仅更改先前的7。有人可以看到我在做什么吗?

这是数组的初始化:
myDays = [[NSMutableArray alloc] init];
NSCalendar *cal = [NSCalendar autoupdatingCurrentCalendar];
NSDate *today = [NSDate date];
date = [NSDate date];




for (int i = 0; i < 31; i++)
{
NSDateComponents *comps = [[NSDateComponents alloc]init];
[comps setDay:i];
[myDays addObject:[cal dateByAddingComponents:comps toDate:today options:0]];
}

最佳答案

尝试以下方法:

NSDate *currentDate = [NSDate date];
NSDate *dateSevenDaysPrior = [currentDate dateByAddingTimeInterval:-(7 * 24 * 60 * 60)];
NSDate *someDate = [currentDate dateByAddingTimeInterval:-(3 * 24 * 60 * 60)]; // adjust as you like

if (([dateSevenDaysPrior compare:someDate] != NSOrderedDescending) &&
([someDate compare:currentDate] != NSOrderedDescending))
{
NSLog(@"date is in range");
}
else
{
NSLog(@"date is not in range");
}

假设您提供的内容(此示例中 someDate代表的意思)确实是NSDate的实例。

关于ios - 检查日期是否在范围内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15033769/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com