NSDate
NSDateFormatter
과 NSCalendar
에 대해 많은 연구를 해보았지만 한 달의 특정 날을 찾는 방법을 찾을 수 없습니다. 예 : 12 월 2 일 월요일 (10/12/2012
)의 날짜를 찾고 싶습니다. 나는 주 수나 달 수를 찾는 방법을 알고 있지만 그것을 할 수있는 방법을 알아낼 수는 없습니다.한달의 특정 요일을 찾는 방법?
감사합니다.
NSDate
NSDateFormatter
과 NSCalendar
에 대해 많은 연구를 해보았지만 한 달의 특정 날을 찾는 방법을 찾을 수 없습니다. 예 : 12 월 2 일 월요일 (10/12/2012
)의 날짜를 찾고 싶습니다. 나는 주 수나 달 수를 찾는 방법을 알고 있지만 그것을 할 수있는 방법을 알아낼 수는 없습니다.한달의 특정 요일을 찾는 방법?
감사합니다.
Robin과 Gill의 답변을 통해 문제를 해결할 수있었습니다. 좀 더 편집하면 내 문제에 대한 해결책을 얻을 수있었습니다. 여기
내가 당기의 세번째 월요일 솔루션-(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];
시도는
다음 링크를 참조하시기 바랍니다 :)뿐만 아니라 달 동안 적용이 코드에서 일주일의 특정 일을 찾을 수 있습니다 : -
How do I get the day of the week with Cocoa Touch?
Number of days in the current month using iPhone SDK?
희망 도움말 :
감사합니다 길 : 나에게 많은 도움 않았다 이 .. 다른 –
@AdityaMathur 우르 오신 것을 환영합니다 ... 아무것도 알려 : 그것은했다 – IronManGill
// 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;
}
당신에게 로빈 감사 주시기 바랍니다 원하는 이잖아 나를 위해 모든 속임수 ... –
입력은 " 12 월 ", 출력은"2012 년 10 월 12 일 "이어야합니다. 이게 니가 원하는거야? –
[NSDate의 날짜, 월 및 연도를 정수 형식으로 얻는 방법?] (http://stackoverflow.com/questions/6214094/how-to-get-nsdate-day-month-and-year-in- 정수 형식) –
@ParamasivanSamuttiram 내가 정확히 –