我想創建一個函數,導致下週五的日期,但我沒有計劃如何去做。有沒有人給我一個好的提示?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. 週五下午根據另一個
- 3. 按下按鈕時,如何使用nscalendar&nsdate進入下一週?
- 4. 週五加7天?
- 5. Java日曆計算奇數週的下週五
- 6. iPhone NSDate setDay
- 7. iphone NSDate問題
- 8. iPhone SDK,如何獲取即將到來的星期五20:00的NSDate對象?
- 9. 週一起始順序,週五結束
- 10. 排除週五和週六,而在SSRS
- 11. 如何轉換以下到週一至週五使用Java代碼
- 12. NSDate返回小時/天/周
- 13. 根據周/年的nsdate組
- 14. NSDate零秒沒有四捨五入
- 15. 獲取週五在VBA
- 16. iPhone - NSDate之間的轉換 - NSString - NSDate
- 17. iPhone:的NSString的NSDate
- 18. core-plot and NSDate(iPhone)
- 19. iPhone SDK NSString到NSDate
- 20. 從iPhone的NSString NSDate
- 21. Informix查詢EXCLUDE本週的週五和週六以及週四和週日的下午4點到8點
- 22. SQL Server:SELECT上週五下午12點到這個星期五12 PM
- 23. 報警在特定日子不是每天例如重複(週日,週二,週五)
- 24. 如何使用JQ運行JavaScript代碼週一至週五
- 25. 如何顯示兩天以內(週一和週五)的平日?
- 26. SQL Server:週末以週五和上週末爲結束日期
- 27. PHP MySQL 7日,周,週一至週五的日曆分頁?
- 28. HTML將日期動態更改爲週一,週三或週五
- 29. 獲取本週的週一和週五的日期(PHP)
- 30. 週五和週四禁用週日Jquery DatePicker
[將組件添加到日期](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