如何获得目标c中每月的第n个星期日日期? [英] How to get n-th Sunday date of the month in objective c?

查看:110
本文介绍了如何获得目标c中每月的第n个星期日日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

例如: - 我只想显示友谊日期

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

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

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.

我需要最好的逻辑来找到第n个星期日的日期。请告诉逻辑,谢谢

I need best logic for finding the date of nth sunday. Please tell the logic, Thanks

推荐答案

您需要创建一个NSDateComponents对象并设置相应的值
你可以这样做:

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

要将其转换为NSDate-你只需要:

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 ;)

这篇关于如何获得目标c中每月的第n个星期日日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆