Nag*_*h M 4 iphone calendar objective-c ios4 ios
如何在对象C编码中获得第一个星期日或第n个星期日或星期一(任何一天)日期.
例如: - 我只想每年在我的应用程序中显示友谊日的日期.
但是,友谊来自8月的第一个星期日.所以,日期将每年都在变化.在这里,我需要找到每年8月的第一个星期日的日期.
我需要最好的逻辑来找到第n个星期日的日期.请说出逻辑,谢谢
您需要创建一个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
Run Code Online (Sandbox Code Playgroud)
要将其转换为NSDate对象,您只需执行以下操作:
//you may want to use another calendar object here
NSDate *myDate = [[NSCalendar currentCalendar] dateFromComponents:dateComponents];
[dateComponents release]; //don't forget memory management ;)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2824 次 |
| 最近记录: |