仅获取两个日期之间的周末

发布于 2024-12-28 22:59:00 字数 2164 浏览 1 评论 0原文

我试图只获取两个日期之间的周六和周日,但我不知道为什么让我一周有空闲的日子。

这是我的代码:

- (BOOL)checkForWeekend:(NSDate *)aDate {
    BOOL isWeekendDate = NO;
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSRange weekdayRange = [calendar maximumRangeOfUnit:NSWeekdayCalendarUnit];
    NSDateComponents *components = [calendar components:NSWeekdayCalendarUnit fromDate:aDate];
    NSUInteger weekdayOfDate = [components weekday];

    if (weekdayOfDate == weekdayRange.location || weekdayOfDate == weekdayRange.length) {
        // The date falls somewhere on the first or last days of the week.
        isWeekendDate = YES;
    }
    return isWeekendDate;
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    NSString *strDateIni = [NSString stringWithString:@"28-01-2012"];
    NSString *strDateEnd = [NSString stringWithString:@"31-01-2012"];

    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"dd-MM-yyyy"];
    NSDate *startDate = [df dateFromString:strDateIni];
    NSDate *endDate = [df dateFromString:strDateEnd];

    unsigned int unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit;

    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *comps = [gregorian components:unitFlags fromDate:startDate  toDate:endDate  options:0];

   // int months = [comps month];
    int days = [comps day];

    for (int i=0; i<days; i++) 
    {

        NSTimeInterval interval = i;
        NSDate * futureDate = [startDate dateByAddingTimeInterval:interval];

        BOOL isWeekend = [self checkForWeekend:futureDate]; // Any date can be passed here.

        if (isWeekend) {
            NSLog(@"Weekend date! Yay!");
        }
        else
        {
            NSLog(@"Not is Weekend");
        }


    }

}

问题: 该问题是由 NSTimeInterval Interval = i; 引起的,for 循环的逻辑是按天迭代。将时间间隔设置为 i 是以秒为单位迭代的。

来自 NSTimeInterval 的文档

NSTimeInterval 始终以秒为单位指定;

答案:

NSTimeInterval 行更改为

NSTimeInterval interval = i*24*60*60;

I'm trying get only the Saturdays and Sundays between two dates, but I don't know why get me free days on a week.

Here is my code:

- (BOOL)checkForWeekend:(NSDate *)aDate {
    BOOL isWeekendDate = NO;
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSRange weekdayRange = [calendar maximumRangeOfUnit:NSWeekdayCalendarUnit];
    NSDateComponents *components = [calendar components:NSWeekdayCalendarUnit fromDate:aDate];
    NSUInteger weekdayOfDate = [components weekday];

    if (weekdayOfDate == weekdayRange.location || weekdayOfDate == weekdayRange.length) {
        // The date falls somewhere on the first or last days of the week.
        isWeekendDate = YES;
    }
    return isWeekendDate;
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    NSString *strDateIni = [NSString stringWithString:@"28-01-2012"];
    NSString *strDateEnd = [NSString stringWithString:@"31-01-2012"];

    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"dd-MM-yyyy"];
    NSDate *startDate = [df dateFromString:strDateIni];
    NSDate *endDate = [df dateFromString:strDateEnd];

    unsigned int unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit;

    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *comps = [gregorian components:unitFlags fromDate:startDate  toDate:endDate  options:0];

   // int months = [comps month];
    int days = [comps day];

    for (int i=0; i<days; i++) 
    {

        NSTimeInterval interval = i;
        NSDate * futureDate = [startDate dateByAddingTimeInterval:interval];

        BOOL isWeekend = [self checkForWeekend:futureDate]; // Any date can be passed here.

        if (isWeekend) {
            NSLog(@"Weekend date! Yay!");
        }
        else
        {
            NSLog(@"Not is Weekend");
        }


    }

}

The problem:
The issue was caused by NSTimeInterval interval = i; The logic of the for loop was to iterate by days. Setting the time interval to i was iterating by seconds.

From documentation on NSTimeInterval

NSTimeInterval is always specified in seconds;

The answer:

Changing the NSTimeInterval line to

NSTimeInterval interval = i*24*60*60;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我不在是我 2025-01-04 22:59:00

这是我在 SO 上发布的另一个答案的链接(无耻,我知道)。它有一些代码可以帮助您确定未来的日期。这些方法作为 NSDate 的类别实现,这意味着它们成为 NSDate 的方法。

那里有一些功能可以帮助您度过周末。但这两个可能是最有帮助的:

- (NSDate*) theFollowingWeekend;
- (NSDate *) thePreviousWeekend;

它们返回接收者(自己)之前和之后周末的日期。

一般来说,你不应该使用一天是 86400 秒的概念,而应该使用 NSDateComponents 和 NSCalendar。即使日期之间发生夏令时转换,此功能也有效。像这样:

- (NSDate *) dateByAddingDays:(NSInteger) numberOfDays {
    NSDateComponents *dayComponent = [[NSDateComponents alloc] init];
    dayComponent.day = numberOfDays;

    NSCalendar *theCalendar = [NSCalendar currentCalendar];
    return [theCalendar dateByAddingComponents:dayComponent toDate:self options:0];
}

Here is a link to another answer I posted on SO (shameless, I know). It has some code that may help you with dates in the future. The methods are implemented as categories of NSDate, meaning they become methods of NSDate.

There are several functions there that help with weekends. But these two might be most helpful:

- (NSDate*) theFollowingWeekend;
- (NSDate *) thePreviousWeekend;

They return the date of the weekend following and prior to the receiver (self).

Generally, you should not use the notion that a day is 86400 seconds, and should use NSDateComponents and NSCalendar. This works even when daylight savings time transitions occur between dates. Like this:

- (NSDate *) dateByAddingDays:(NSInteger) numberOfDays {
    NSDateComponents *dayComponent = [[NSDateComponents alloc] init];
    dayComponent.day = numberOfDays;

    NSCalendar *theCalendar = [NSCalendar currentCalendar];
    return [theCalendar dateByAddingComponents:dayComponent toDate:self options:0];
}
顾北清歌寒 2025-01-04 22:59:00

要记住的一件非常重要的事情是,一天并不(必然)等于 24*60*60 秒。而且你不应该自己做日期算术

你真正需要做的事情可能看起来有点乏味,但这是正确的做法:使用 NSCalendar– dateByAddingComponents:toDate:options:

参见日历计算指南。

One very important thing to remember is that one day is not (necessarily) equal to 24*60*60 seconds. And you should not do date arithmetic yourself

What you really need to do might seem a little tedious but this is the correct thing to do: use NSCalendar and – dateByAddingComponents:toDate:options:

See Calendrical Calculations guide.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文