2011-03-18 73 views
13

你好 我有設置firstWeekDay一個問題,這裏是我做的:獲取第一天一個星期與NSDateComponents:改變今年

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
comp = [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSWeekCalendarUnit | NSWeekdayCalendarUnit) fromDate:targetDate]; 
[comp setWeekday:2]; 

NSDate *firstWeekDay = [gregorian dateFromComponents:comp]; 

如果週六2011-01-01是targetDate,該firstWeekDay在我的日曆中似乎是2011-12-26星期一,但實際上應該是2010-12-26星期一。我怎樣才能做到這一點?

+0

這是有可能,爲什麼在iOS上的鬧鐘沒有正確地爲某些用戶在工作2011-01-01原因? – Felix 2011-03-18 09:00:40

回答

1

試試這個

// adjust them for first day of previous week (Monday) 
[comp setWeekday:2]; 
[comp setWeek:([comp week] - 1)]; 

注意此解決方案假定一週的第一天是星期一。

+0

謝謝,但你的方法不起作用。 firstWeekDay現在成爲2011-12-20 – Roman 2011-03-18 09:17:58

+0

是的,適合我。 – 2013-10-28 03:37:58

35

要解決此問題,請在NSDateComponents中添加NSYearForWeekOfYearCalendarUnit

例:

NSCalendar* calendar = [NSCalendar currentCalendar]; 
NSDateComponents* comps = [calendar components:NSYearForWeekOfYearCalendarUnit |NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:curDate]; 

[comps setWeekday:2]; // 2: monday 
firstDayOfTheWeek = [calendar dateFromComponents:comps]; 
[comps setWeekday:7]; // 7: saturday 
lastDayOfTheWeek = [calendar dateFromComponents:comps]; 
+3

使我的一天,這應該被接受的答案。 – 2013-02-26 09:27:16

+3

這是解決方案。如果你仍然有一些問題,並使用NSDayCalendarUnit然後嘗試刪除它。 – altosh 2013-06-14 04:03:19

+0

爲什麼你需要「NSYearForWeekOfYearCalendarUnit」?它有什麼作用? – shim 2013-11-25 14:19:33

1

下面還覆蓋邊緣的情況下,

- (NSDate *)getFirstDayOfTheWeekFromDate:(NSDate *)givenDate 
{ 
    NSCalendar *calendar = [NSCalendar currentCalendar]; 


    NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:givenDate]; 
    [components setWeekday:2]; // 1 == Sunday, 7 == Saturday 
    if([[calendar dateFromComponents:components] compare: curDate] == NSOrderedDescending) // if start is later in time than end 
    { 
     [components setWeek:[components week]-1]; 
    } 

    return [calendar dateFromComponents:components]; 
} 
1

雨燕解決方案(感謝@YvesJusot):

let calendar = NSCalendar.currentCalendar() 
let comps = calendar.components(NSCalendarUnit.WeekOfYearCalendarUnit|NSCalendarUnit.YearCalendarUnit|NSCalendarUnit.MonthCalendarUnit|NSCalendarUnit.WeekCalendarUnit|NSCalendarUnit.WeekdayCalendarUnit, fromDate: NSDate()) as NSDateComponents 

comps.setValue(2, forComponent: NSCalendarUnit.WeekdayCalendarUnit) 
let monday = calendar.dateFromComponents(comps)! 

println("\(monday)") 
0

我的確在@JasonGarrett一些變化答案,它適用於我。

let calendar = NSCalendar.currentCalendar() 
let flags : NSCalendarUnit = NSCalendarUnit.CalendarUnitDay | NSCalendarUnit.CalendarUnitMonth | NSCalendarUnit.CalendarUnitYear 
let comps = calendar.components(flags, fromDate: NSDate()) as NSDateComponents 
       comps.setValue(2, forComponent: NSCalendarUnit.CalendarUnitWeekday) 
let monday = calendar.dateFromComponents(comps)! 
1

假設在星期一使用動態值而不是硬編碼2!

NSCalendar* calendar = [NSCalendar currentCalendar]; 
NSDateComponents* comps = [calendar components:NSYearForWeekOfYearCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:[NSDate date]]; 

comps.weekday = calendar.firstWeekday; 
firstDayOfTheWeek = [calendar dateFromComponents:comps]; 
comps.weekday = calendar.firstWeekday - 1; 
lastDayOfTheWeek = [calendar dateFromComponents:comps]; 
0
// Make these two methods as categories to NSDate 
// Use This to get First date of the week (Monday) 

- (NSDate *) getFirstDayOfWeek { 

    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; 

    NSDateComponents *compOfDate = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday) fromDate:self]; 

    NSInteger numOfDaysToAdd = compOfDate.weekday == 1 ? -6 : (2 - compOfDate.weekday); 

    NSDateComponents *compToAdd = [[NSDateComponents alloc] init]; 

    compToAdd.day = numOfDaysToAdd; 

    return [calendar dateByAddingComponents:compToAdd toDate:self options:0]; 
} 

// Use This to get last date of the week (Sunday) 

- (NSDate *)getLastDayOfWeek { 

    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; 

    NSDateComponents *compOfDate = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday) fromDate:self]; 

    NSInteger numOfDaysToAdd = compOfDate.weekday == 1 ? 0 : (8 - compOfDate.weekday); 

    NSDateComponents *compToAdd = [[NSDateComponents alloc] init]; 

    compToAdd.day = numOfDaysToAdd; 

    return [calendar dateByAddingComponents:compToAdd toDate:self options:0]; 
} 
相關問題