计算“上周五”带 NSDate + NSDate组件

发布于 2024-10-10 11:30:49 字数 726 浏览 2 评论 0原文

我在使用 NSDate 和 NSDateComponents 计算相对于当前日期的特定日期时遇到一些问题。例如,假设我想要上周五。我尝试了以下操作:

    NSDateComponents *components = [[NSCalendar currentCalendar] components:(NSYearCalendarUnit| NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekCalendarUnit |  NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekdayCalendarUnit | NSWeekdayOrdinalCalendarUnit) fromDate:[NSDate date]];

[components setHour:0];
[components setMinute:0];
[components setSecond:0];
[components setWeekday:6]; //Friday
[components setWeek:[components week] - 1]; //Last Week

return [[NSCalendar currentCalendar] dateFromComponents:components];

但这只是返回当前日期,好像它无法弄清楚我要的是什么。

这感觉应该很简单,但我只是没有让它发挥作用。有什么指点吗?

I'm having some trouble computing specific days relative to the current date using NSDate and NSDateComponents. For example, let's say I want last Friday. I tried the following:

    NSDateComponents *components = [[NSCalendar currentCalendar] components:(NSYearCalendarUnit| NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekCalendarUnit |  NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekdayCalendarUnit | NSWeekdayOrdinalCalendarUnit) fromDate:[NSDate date]];

[components setHour:0];
[components setMinute:0];
[components setSecond:0];
[components setWeekday:6]; //Friday
[components setWeek:[components week] - 1]; //Last Week

return [[NSCalendar currentCalendar] dateFromComponents:components];

But that just returns the current date, as if it can't figure out what I'm asking for.

This feels like it should be simple enough but I'm just not getting it working. Any pointers?

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

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

发布评论

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

评论(3

蘸点软妹酱 2024-10-17 11:30:50

像这样的事情怎么样:

NSDate* today = [NSDate date];
NSDateComponents* comps = [[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:today];

// do the modulo arithmetic to determine the delta days to last Friday
NSInteger weekday = [comps weekday];
NSInteger deltaDays = 0; // left as exercise, I am hungry for lunch...

NSDateComponents* delta = [[NSDateComponents alloc] init];
[delta setDay:deltaDays];
NSDate* lastFriday = [[NSCalendar currentCalendar] dateByAddingComponents:delta toDate:today options:0];

[delta release];

How about something like this:

NSDate* today = [NSDate date];
NSDateComponents* comps = [[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:today];

// do the modulo arithmetic to determine the delta days to last Friday
NSInteger weekday = [comps weekday];
NSInteger deltaDays = 0; // left as exercise, I am hungry for lunch...

NSDateComponents* delta = [[NSDateComponents alloc] init];
[delta setDay:deltaDays];
NSDate* lastFriday = [[NSCalendar currentCalendar] dateByAddingComponents:delta toDate:today options:0];

[delta release];
↘人皮目录ツ 2024-10-17 11:30:50

使用此代码:

- (NSDate *)lastDayOfMonthWithDate:(NSDate *)date
{
    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSYearCalendarUnit | NSMonthCalendarUnit fromDate:date];
    components.month += 1;
    components.day = 0;

    NSDate *lastDayOfMonth = [[NSCalendar currentCalendar] dateFromComponents:components];

    return lastDayOfMonth;
}

- (NSDate *)lastFridayOfMonthWithDate:(NSDate *)date
{
    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit fromDate:[self lastDayOfMonthWithDate:date]];

    if (components.weekday >= 6) {
        components.day -= components.weekday - 6;
    } else {
        components.day +=  6 - components.weekday - 7;
    }

    NSDate *lastFridayOfMonth = [[NSCalendar currentCalendar] dateFromComponents:components];

    return lastFridayOfMonth;
}

Use this code:

- (NSDate *)lastDayOfMonthWithDate:(NSDate *)date
{
    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSYearCalendarUnit | NSMonthCalendarUnit fromDate:date];
    components.month += 1;
    components.day = 0;

    NSDate *lastDayOfMonth = [[NSCalendar currentCalendar] dateFromComponents:components];

    return lastDayOfMonth;
}

- (NSDate *)lastFridayOfMonthWithDate:(NSDate *)date
{
    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit fromDate:[self lastDayOfMonthWithDate:date]];

    if (components.weekday >= 6) {
        components.day -= components.weekday - 6;
    } else {
        components.day +=  6 - components.weekday - 7;
    }

    NSDate *lastFridayOfMonth = [[NSCalendar currentCalendar] dateFromComponents:components];

    return lastFridayOfMonth;
}
盛夏尉蓝 2024-10-17 11:30:50

我写了这个函数:

- (NSInteger)lastOccurrenceOfWeekdayWithNumber:(NSInteger)weekdayNumber inMonthForDate:(NSDate *)date {
NSCalendarUnit units = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitWeekdayOrdinal;
NSRange range = [_calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date];
NSDateComponents *dateComponents = [_calendar components:units fromDate:date];
dateComponents.day = range.length;

date = [_calendar dateFromComponents:dateComponents];
dateComponents = [_calendar components:units fromDate:date];

while (dateComponents.weekday != weekdayNumber) {
    dateComponents.day -= 1;
    date = [_calendar dateFromComponents:dateComponents];
    dateComponents = [_calendar components:units fromDate:date];
}

return dateComponents.weekdayOrdinal;
}

当你寻找星期日时,weekdayNumber 是 1。 2 周一。

如果您想查找 10 月的最后一个星期五,可以使用以下命令:

[self lastOccurrenceOfWeekdayWithNumber:6 inMonthForDate:date];

其中日期是 2016 年 10 月 11 日或 10 月的任何其他一天。

I wrote this function:

- (NSInteger)lastOccurrenceOfWeekdayWithNumber:(NSInteger)weekdayNumber inMonthForDate:(NSDate *)date {
NSCalendarUnit units = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitWeekdayOrdinal;
NSRange range = [_calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date];
NSDateComponents *dateComponents = [_calendar components:units fromDate:date];
dateComponents.day = range.length;

date = [_calendar dateFromComponents:dateComponents];
dateComponents = [_calendar components:units fromDate:date];

while (dateComponents.weekday != weekdayNumber) {
    dateComponents.day -= 1;
    date = [_calendar dateFromComponents:dateComponents];
    dateComponents = [_calendar components:units fromDate:date];
}

return dateComponents.weekdayOrdinal;
}

weekdayNumber is 1 when you are looking for Sunday. 2 for Monday.

If you want to find last Friday in October you can use this:

[self lastOccurrenceOfWeekdayWithNumber:6 inMonthForDate:date];

where date is let's say the 11th of October, 2016 or any other day of October.

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