뭔가
- (NSString *)monthNameForWeek:(NSUInteger)week inYear:(NSInteger)year {
NSDateComponents * dateComponents = [NSDateComponents new];
dateComponents.year = year;
dateComponents.weekOfYear = week;
dateComponents.weekday = 1; // 1 indicates the first day of the week, which depends on the calendar
NSDate * date = [[NSCalendar currentCalendar] dateFromComponents:dateComponents];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MMMM"];
return [formatter stringFromDate:date];
}
참고 할 것입니다.
사용자의 요구에 맞지 않는 경우 NSCalendar
인스턴스를 제공하고 currentCalendar
대신 날짜를 검색하는 데 사용할 수 있습니다. 이렇게하면 일주일 중 첫 번째 날과 같은 일을 구성 할 수 있습니다. documentation이 NSCalendar
이면 읽을 가치가 있습니다. 사용자 정의 달력을 사용하는 일반적인 경우라면 당신은 반환하지 않는
, 그냥, 당신이 방법 이름을 get
을 피해야한다,
- (NSString *)monthNameForWeek:(NSUInteger)week inYear:(NSInteger)year {
[self monthNameForWeek:week inYear:year calendar:[NSCalendar currentCalendar]];
}
- (NSString *)monthNameForWeek:(NSUInteger)week inYear:(NSInteger)year calendar:(NSCalendar *)calendar {
NSDateComponents * dateComponents = [NSDateComponents new];
dateComponents.year = year;
dateComponents.weekOfYear = week;
dateComponents.weekday = 1; // 1 indicates the first day of the week, which depends on the calendar
NSDate * date = [calendar dateFromComponents:dateComponents];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MMMM"];
return [formatter stringFromDate:date];
}
관련이없는 보조 노트로
처럼 뭔가 구현을 변경 간접적으로 가치.
NSDateComponents 유용 할 수 있습니다. – Larme
'setDateFormat' 대신 NSDateFormatter'monthSymbols'를 사용하십시오. https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html#//apple_ref/occ/instm/NSDateFormatter/monthSymbols –