在 Objective-C 中获取下周六的日期?

发布于 2024-10-10 15:43:10 字数 47 浏览 2 评论 0原文

在 iOS 的 Objective-C 中,如何获取今天之后第一个星期六的日期?

How would you get the date of the the first saturday that occurs after today in Objective-C for iOS?

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

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

发布评论

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

评论(3

诺曦 2024-10-17 15:43:10
NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *date = [NSDate date];
NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit fromDate:date];
NSInteger todayWeekday = [weekdayComponents weekday];

enum Weeks {
    SUNDAY = 1,
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY
};

NSInteger moveDays=SATURDAY-todayWeekday;
if (moveDays<=0) {
    moveDays+=7;
}

NSDateComponents *components = [NSDateComponents new];
components.day=moveDays;

NSCalendar *calendar=[[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
NSDate* newDate = [calendar dateByAddingComponents:components toDate:date options:0];

NSLog(@"%@",newDate);
NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *date = [NSDate date];
NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit fromDate:date];
NSInteger todayWeekday = [weekdayComponents weekday];

enum Weeks {
    SUNDAY = 1,
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY
};

NSInteger moveDays=SATURDAY-todayWeekday;
if (moveDays<=0) {
    moveDays+=7;
}

NSDateComponents *components = [NSDateComponents new];
components.day=moveDays;

NSCalendar *calendar=[[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
NSDate* newDate = [calendar dateByAddingComponents:components toDate:date options:0];

NSLog(@"%@",newDate);
〆凄凉。 2024-10-17 15:43:10

[NSDate dateWithNaturalLanguageString: @"next Saturday"]


编辑:从 OS X 10.9 和 iOS 8 开始,您可以执行以下操作:

NSCalendar *calendar = [NSCalendar currentCalendar];

NSDate *now = [NSDate date];
NSDateComponents *saturday = [[NSDateComponents alloc] init];
saturday.weekday = 7 /* Saturday */;

NSDate *nextSaturday = [calendar nextDateAfterDate:now matchingComponents:saturday options:0];

[NSDate dateWithNaturalLanguageString: @"next Saturday"]


Edit: Starting in OS X 10.9 and iOS 8, you can do this:

NSCalendar *calendar = [NSCalendar currentCalendar];

NSDate *now = [NSDate date];
NSDateComponents *saturday = [[NSDateComponents alloc] init];
saturday.weekday = 7 /* Saturday */;

NSDate *nextSaturday = [calendar nextDateAfterDate:now matchingComponents:saturday options:0];
哥,最终变帅啦 2024-10-17 15:43:10

这将指导您完成操作日历日期的过程:

日历计算

This will guide you through the process of manipulating calendar dates:

Calendrical Calculations

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