如何在目标c中获得该月的第n个星期日日期?

发布于 2024-12-10 17:52:54 字数 183 浏览 1 评论 0原文

如何在 Objective C 编码中获取第一个星期日或第 n 个星期日或星期一(任意一天)日期。

例如:- 我只想每年在我的应用程序中显示友谊日的日期。

但是,友谊是在八月的第一个星期日。所以,日期每年都会改变。在这里我需要找到每年八月的第一个星期日的日期。

有没有逻辑可以找到第 n 个星期日的日期。

How to get first sunday or nth sunday or monday (any day) date in Objective C coding.

For example :- I just want to show date of friendship day in my app every year.

But, friendship comes 1st Sunday of Aug. So, date will change every year. here I need to find what is the date of 1st Sunday in Aug every year.

is there any logic to find the date of nth Sunday.

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

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

发布评论

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

评论(5

熊抱啵儿 2024-12-17 17:52:54

您需要创建一个 NSDateComponents 对象并设置适当的值,
在您的示例中,您将执行以下操作:

NSDateComponents *dateComponents = [[NSDateComponents alloc] init];

dateComponents.year = 2011; // set the current year or whatever year you want here
dateComponents.month = 8;
dateComponents.weekday = 1; // sunday is 1, monday is 2, ...
dateComponents.weekdayOrdinal = 1; // this means, the first of whatever weekday you specified

要将其转换为 NSDate 对象,您只需执行以下操作:

//you may want to use another calendar object here
NSDate *myDate = [[NSCalendar currentCalendar] dateFromComponents:dateComponents];
[dateComponents release]; //don't forget memory management ;)

You need to create a NSDateComponents object and set the appropriate values,
In You example you would do:

NSDateComponents *dateComponents = [[NSDateComponents alloc] init];

dateComponents.year = 2011; // set the current year or whatever year you want here
dateComponents.month = 8;
dateComponents.weekday = 1; // sunday is 1, monday is 2, ...
dateComponents.weekdayOrdinal = 1; // this means, the first of whatever weekday you specified

To convert this into a NSDate-object, you just do:

//you may want to use another calendar object here
NSDate *myDate = [[NSCalendar currentCalendar] dateFromComponents:dateComponents];
[dateComponents release]; //don't forget memory management ;)
旧时浪漫 2024-12-17 17:52:54

您希望将 NSDateComponentsNSCalendardateFromComponents: 方法结合使用:

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *c = [[NSDateComponents alloc] init];
[c setYear:2012];
[c setWeekday:1]; // Sunday
[c setWeekdayOrdinal:3]; // The 3rd Sunday

for (int i = 1; i <= 12; i++) {
    [c setMonth:i];
    NSDate *date = [gregorian dateFromComponents:c];
    NSLog(@"Date[%i]: %@",i,date);
}

[c release];
[gregorian release];

You want to use NSDateComponents in combination with NSCalendar's dateFromComponents: method:

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *c = [[NSDateComponents alloc] init];
[c setYear:2012];
[c setWeekday:1]; // Sunday
[c setWeekdayOrdinal:3]; // The 3rd Sunday

for (int i = 1; i <= 12; i++) {
    [c setMonth:i];
    NSDate *date = [gregorian dateFromComponents:c];
    NSLog(@"Date[%i]: %@",i,date);
}

[c release];
[gregorian release];
淡墨 2024-12-17 17:52:54

这是一个例子。您可以在 日期和时间编程指南

查找八月的第一个星期日:

NSDateComponents *components = [[NSDateComponents alloc] init];
[components setWeekday:1]; // Sunday
[components setWeekdayOrdinal:1]; // The first Sunday in the month
[components setMonth:8]; // August
[components setYear:2011];
NSCalendar *gregorian = [[NSCalendar alloc]
                         initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *date = [gregorian dateFromComponents:components];

Here's an example. You can find more information in the Date and Time Programming Guide.

To find the first Sunday in August:

NSDateComponents *components = [[NSDateComponents alloc] init];
[components setWeekday:1]; // Sunday
[components setWeekdayOrdinal:1]; // The first Sunday in the month
[components setMonth:8]; // August
[components setYear:2011];
NSCalendar *gregorian = [[NSCalendar alloc]
                         initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *date = [gregorian dateFromComponents:components];
信愁 2024-12-17 17:52:54

您检查该月的第一天。

unsigned units = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorian components:units fromDate:date];

使用 [components day] 获取日期; <-1 表示周日。之后就很简单了。

you check the first day of the month.

unsigned units = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorian components:units fromDate:date];

Get the day using [components day]; <-1 for Sunday.After this it's simple.

旧时浪漫 2024-12-17 17:52:54

您需要查看 NSCalendar此处。

特别感兴趣的是方法 - (NSUInteger)ordinalityOfUnit:(NSCalendarUnit)较小的 inUnit:(NSCalendarUnit)较大的 forDate:(NSDate *)date

You'll want to review the NSCalendar class here.

Of specific interest will be the method - (NSUInteger)ordinalityOfUnit:(NSCalendarUnit)smaller inUnit:(NSCalendarUnit)larger forDate:(NSDate *)date

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