我想创建一个函数,导致下周五的日期,但我没有计划如何去做。有没有人给我一个好的提示?iPhone NSDate例如。下周五
回答
E.g.使用NSDate获取当前日期,然后使用NSCalendar的'components> fromDate:'获取NSDateComponents,然后将时间差添加到下一个星期五,并创建一个新的NSDate,Bob's是您的叔叔。
我会试着找出那条路线。欢呼声 – 2010-05-22 01:10:26
这是我的解决方案,只是为了提醒你,星期六是星期五展示之前。 欢呼所有
NSDate *today = [[NSDate alloc] init];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit fromDate:today];
int weekday = [weekdayComponents weekday];
int iOffsetToFryday = -weekday + 6;
weekdayComponents.weekday = iOffsetToFryday;
NSDate *nextFriday = [[NSCalendar currentCalendar] dateByAddingComponents:weekdayComponents toDate:today options:0];
如果在星期六运行时返回错误的答案,您怎么认为这是正确的? – 2010-05-24 20:18:14
我看到这不是正确的方式来处理 – 2010-06-20 23:55:47
这里是获取未来5日在公历我工作的解决方案:
self.nextBeginDates = [NSMutableArray array];
NSDateComponents *weekdayComponents = [[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:[NSDate date]];
int currentWeekday = [weekdayComponents weekday]; //[1;7] ... 1 is sunday, 7 is saturday in gregorian calendar
NSDateComponents *comp = [[NSDateComponents alloc] init];
[comp setDay:8 - currentWeekday]; // add some days so it will become sunday
// build weeks array
int weeksCount = 5;
for (int i = 0; i < weeksCount; i++) {
[comp setWeek:i]; // add weeks
[nextBeginDates addObject:[[NSCalendar currentCalendar] dateByAddingComponents:comp toDate:[NSDate date] options:0]];
}
[comp release];
这应该工作
+ (NSDate *) dateForNextWeekday: (NSInteger)weekday {
NSDate *today = [[NSDate alloc] init];
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
// Get the weekday component of the current date
NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit
fromDate:today];
/*
Add components to get to the weekday we want
*/
NSDateComponents *componentsToSubtract = [[NSDateComponents alloc] init];
NSInteger dif = weekday-weekdayComponents.weekday;
if (dif<=0) dif += 7;
[componentsToSubtract setDay:dif];
NSDate *beginningOfWeek = [gregorian dateByAddingComponents:componentsToSubtract
toDate:today options:0];
return beginningOfWeek;
}
根据问题的要求,这种情况下实际存在一个错误。如果您正在尝试获取当前工作日的下一个实例(即星期五的下周五),则最终会提供当前日期。解决的办法是将'dif <0'改为'dif <= 0'。 – Dima 2013-05-17 17:43:18
保持它简单,安全,可读! (.... KISSAR?)
#define FRIDAY 6
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dayComponent = [[NSDateComponents alloc] init];
dayComponent.day = 1;
NSDate *nextFriday = [NSDate date];
NSInteger iWeekday = [[gregorian components:NSWeekdayCalendarUnit fromDate:nextFriday] weekday];
while (iWeekday != FRIDAY) {
nextFriday = [gregorian dateByAddingComponents:dayComponent toDate:nextFriday options:0];
iWeekday = [[gregorian components:NSWeekdayCalendarUnit fromDate:nextFriday] weekday];
}
现在nextFriday
有你的约会。
希望这会有所帮助!
编辑 请注意,如果当前日期已经是星期五,它将返回,而不是下一个星期五。如果这是不可取的,只需在一天之后启动nextFriday
(所以如果当前日期是星期五,它将在星期六开始,迫使下一个星期五。如果当前日期是星期四,您将自动在下个星期五不需要循环)。
- 1. 检查NSDate是否在下周
- 2. 按下按钮时,如何使用nscalendar&nsdate进入下一周?
- 3. 周五加7天?
- 4. iPhone NSDate setDay
- 5. iphone NSDate问题
- 6. iPhone SDK,如何获取即将到来的星期五20:00的NSDate对象?
- 7. 排除周五和周六,而在SSRS
- 8. 周一起始顺序,周五结束
- 9. 如何转换以下到周一至周五使用Java代码
- 10. NSDate返回小时/天/周
- 11. NSDate零秒没有四舍五入
- 12. Informix查询EXCLUDE本周的周五和周六以及周四和周日的下午4点到8点
- 13. iPhone - NSDate之间的转换 - NSString - NSDate
- 14. 报警在特定日子不是每天例如重复(周日,周二,周五)
- 15. iPhone:的NSString的NSDate
- 16. core-plot and NSDate(iPhone)
- 17. iPhone SDK NSString到NSDate
- 18. 如何使用JQ运行JavaScript代码周一至周五
- 19. 如何显示两天以内(周一和周五)的平日?
- 20. PHP MySQL 7日,周,周一至周五的日历分页?
- 21. 获取本周的周一和周五的日期(PHP)
- 22. SQL Server:周末以周五和上周末为结束日期
- 23. 如何替换周六或周日前五周的计算日期
- 24. 列出周一到周五的月份,从周一到周日碳
- 25. 的NSDate返回错误的周数
- 26. iPhone PLIST和NSDate的问题
- 27. iPhone调整NSDate的时间
- 28. 的NSDate - GMT在iPhone上
- 29. iphone的NSDate转换问题
- 30. 为iphone设置nsdate格式
[将组件添加到日期](http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/DatesAndTimes/Articles/dtCalendricalCalculations.html#//apple_ref/doc/uid/TP40007836- SW3)部分的日期和时间编程指南将很有用。清单2的代码可以查找当周的星期天。修改它,以找到下周五。 – DyingCactus 2010-05-22 03:28:33