2012-11-05 45 views
0

我試圖獲取本週星期一至星期四的日期。 但由於某種原因,這段代碼給了我這個星期日期和前幾周的混合。獲取錯誤日期

我知道這可能是一個奇怪的做法,但我真的無法弄清楚代碼有什麼問題。

如果有人得到了越來越日期的更好的方法請給一個提示呢:)

我的代碼:

NSDate *today = [NSDate date]; 


//Change first weekday of the week 

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
gregorian.firstWeekday = 2; 

NSCalendar *gregorian1 = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
gregorian1.firstWeekday = 3; 

NSCalendar *gregorian2 = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
gregorian2.firstWeekday = 4; 

NSCalendar *gregorian3 = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
gregorian3.firstWeekday = 5; 




NSDateComponents *components = [gregorian components:NSWeekCalendarUnit fromDate:today]; 

NSUInteger weekOfYear = [components weekOfYear]; 

//Give the date of the first week day 

NSDate *mondaysdate = nil; 
[gregorian rangeOfUnit:NSWeekCalendarUnit startDate:&mondaysdate interval:NULL forDate:today]; 
NSDate *tuesdaysdate = nil; 
[gregorian1 rangeOfUnit:NSWeekCalendarUnit startDate:&tuesdaysdate interval:NULL forDate:today]; 

NSDate *wednesdaysdate = nil; 
[gregorian2 rangeOfUnit:NSWeekCalendarUnit startDate:&wednesdaysdate interval:NULL forDate:today]; 
NSDate *thursdaysdate = nil; 
[gregorian3 rangeOfUnit:NSWeekCalendarUnit startDate:&thursdaysdate interval:NULL forDate:today]; 


//Making the dates into string 

NSDateFormatter *formatter2 = [[NSDateFormatter alloc]init]; 
formatter2.dateFormat = @"yyyy/MM/dd"; 

NSString *monday = [formatter2 stringFromDate:mondaysdate]; 
NSString *tuesday = [formatter2 stringFromDate:tuesdaysdate]; 
NSString *wednesday = [formatter2 stringFromDate:wednesdaysdate]; 
NSString *thursday = [formatter2 stringFromDate:thursdaysdate]; 

回答

0
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
NSDateComponents *comps = [gregorianCalendar components:(NSYearCalendarUnits | NSMonthCalendarUnits | NSWeekCalendarUnits | NSWeekdayCalendarUnits) fromDate:[NSDate date]]; 

comps.weekday = 2; 
NSDate *monday = [gregorianCalendar dateFromComponents:comps]; 

comps.weekday = 3; 
NSDate *tuesday = [gregorianCalendar dateFromComponents:comps]; 

comps.weekday = 4; 
NSDate *wednesday = [gregorianCalendar dateFromComponents:comps]; 

comps.weekday = 5; 
NSDate *thursday = [gregorianCalendar dateFromComponents:comps]; 
+0

謝謝,這似乎是一個更好的方式來做:)! –

0

您可以使用以下方法來獲得星期幾的數,這是psuedocode,所以可能無法編譯,但它應該給你一個好的開始。根據數字,您可以生成其他日期,然後在稍後返回字符串。

NSDateFormatter *df = [[NSDateFormatter alloc] init]; 
[df setDateFormat:@"c"]; //<- gives you day of week as an integer, 1 for monday, 2 for tuesday, etc 

NSDate *today = [NSDate date]; 
NSInteger dayOfWeek = [[df stringFromDate:today] integerValue]; 

然後創建其他NSDates,通過將日偏移添加到您原來的當前變量。

然後將它們格式化回去。

[df setDateFormat:@"yyyy/mm/dd"]; 
[df stringFromDate:today]; //<-- do this for all the other dates too 

[df release]; //<- if you do this method often, consider having the date formatter as global 

這是格式化的一個很好的參考。 http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns