2012-11-29 39 views
3

我對NSDateNSDateFormatterNSCalendar做了很多研究,但找不到找到一個月的特定日子的方法。 例如, 我想查找12月第2個星期一的日期(10/12/2012)。我知道如何查找週數或月數,但不知道如何去做。如何找到特定的一個月的一天?

謝謝。

+0

輸入是「第2個星期一12月「,輸出應該是」2012年12月12日「。這是你想要的嗎? –

+0

可能重複[如何獲得NSDate日,月和年的整數格式?](http://stackoverflow.com/questions/6214094/how-to-get-nsdate-day-month-and-year-in-整數格式) –

+0

@ParamasivanSamuttiram這就是我到底要 –

回答

0

羅賓和吉爾的回答幫助我解決了我的問題。隨着一些更多的編輯,我能夠得到解決我的問題。

這裏是我用來獲取解決方案

-(void)findWeekDay:(NSInteger)weekDay forWeek:(NSInteger)week OfMonth:(NSInteger)month OfYear:(NSInteger)year 
{ 
     NSDateComponents *dateComp = [[NSDateComponents alloc]init]; 
[dateComp setYear:year]; 
[dateComp setMonth:month]; 
[dateComp setDay:1]; 
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
NSDate *firstDate=[gregorian dateFromComponents:dateComp]; //Sets first date of month. 
firstDate=[self dateToGMT:firstDate]; 
NSLog(@"First date: %@",firstDate); 
NSDateComponents *dateComp1 = [gregorian components:NSDayCalendarUnit|NSWeekdayCalendarUnit fromDate:firstDate]; 
NSInteger firstWeekDay=[dateComp1 weekday]; //Gets firstday of a week. 1-Sunday, 2-Monday and so on. 
NSLog(@"First Week Day: %d",firstWeekDay); 
NSRange daysInMonth = [gregorian rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:firstDate]; //To get number of days in that month 
NSInteger x; //days to be added to first date 
if (firstWeekDay < weekDay) { 
    x = (weekDay - firstWeekDay) + (7 * (week-1)); 
} 
if (firstWeekDay > weekDay) { 
    x = (7 - firstWeekDay + weekDay) + (7 * (week-1)); 
} 
if (firstWeekDay == weekDay) { 
    x = (7 * (week-1)); 
} 
if (x > daysInMonth.length) { 
    NSLog(@"Invalid Date: %d",x); 
} 
else { 
    NSLog(@"Days to be added: %d",x); 
    NSDateComponents *dateComp2 = [[NSDateComponents alloc]init]; 
    [dateComp2 setYear:0]; 
    [dateComp2 setMonth:0]; 
    [dateComp2 setDay:x]; 
    NSDate *desiredDate = [gregorian dateByAddingComponents:dateComp2 toDate:firstDate options:0]; 
    NSLog(@"Your desired date is: %@",desiredDate); 
} 

}

- (NSDate *)dateToGMT:(NSDate *)sourceDate { 
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone]; 
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate]; 
NSDate* destinationDate = [[NSDate alloc] initWithTimeInterval:destinationGMTOffset sinceDate:sourceDate]; 
return destinationDate; 

}

我們找到第三個星期一十二月2012調用方法

代碼
[self findWeekDay:2 forWeek:3 OfMonth:12 OfYear:2012]; 
3
// Set start date of the month you want 
    NSString *str = @"01 - 12 - 2012"; 
    NSDateFormatter *formatter1 = [[[NSDateFormatter alloc] init] autorelease]; 
    [formatter1 setDateFormat:@"dd - MM - yyyy"]; 
    NSDate *date = [formatter1 dateFromString:str]; 
    NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease]; 
    NSDateComponents *weekdayComponents = [gregorian components:(NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:date]; 
    // Get the weekday with sunday as one 
    NSInteger weekday = [weekdayComponents weekday]; 
    int weekNumber = 2; 
    int dayOfweek = 2; 
    int x = (7 - weekday) * (weekNumber - 1) + dayOfweek; 
    // Add the no. of weeks - 1 or - 2 depending on the value of x. 
    // Also Add 1 as the start date is 1st Dec. 
    if(x < 7) 
    { 
     x += 7 * (weekNumber - 1) + 1; 
    } 
    else 
    { 
     x += 7 * (weekNumber - 2) + 1; 
    } 
+1

謝謝羅賓 它做的對我來說整個詭計... –

相關問題