NSDate 는 특정날짜(2001년 1월 1일 자정)를 기준으로 객체에 설정된 날짜 까지의 기간을 초단위로 계산하여 저장하고 있다.
그렇기에 단순한 NSDate 객체로는 년, 월, 일 등의 날짜 값을 얻을 수 없다.
각각의 날짜값을 얻기위해선 다음과 같이 NSCalender 와 NSDateComponents 를 이용해야 한다.
NSDate *now = [NSDate date]; //현재 날짜로 객체 생성
NSCalendar *calender = [NSCalendar currentCalendar]; //캘린더 객체생성
unsigned int calendarFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSWeekCalendarUnit;
//NSYearCalendarUnit, NSMonthCalendarUnit, NSWeekCalendarUnit, NSDayCalendarUnit, NSHourCalendarUnit, NSMinuteCalendarUnit, NSSecondCalendarUnit;
//년, 월, 일, 시, 분, 초 를 각각 얻기위한 플래그들 이 예제는 년,월,일만 사용
NSDateComponents *dateComponent = [calender components:calendarFlags fromDate:now];
//플래그를 이용해 각각의 구성요소를 가져오는 객체
NSLog(@"년 - %d",[dateComponent year]);
NSLog(@"월 - %d",[dateComponent month]);
NSLog(@"일 - %d",[dateComponent day]);
//현재 날짜의 년 월 일이 각각 찍힌다.
플래그를 사용하지 않으면 엉뚱한 값이 도출되니 주의하자.
기타 NSDate에 관련한 여러 내용은 밑의 블로그를 참조. 애플이 제공하는 레퍼런스를 보는게 가장 좋지만... 영어의 압박...ㅠㅠ
http://soooprmx.com/wp/archives/2335
'iOS' 카테고리의 다른 글
[iOS] 코드로 오토레이아웃 조절하기 (0) | 2013.09.25 |
---|---|
[iOS] 번들에서 Document로 파일 복사하기 (sqlite) (0) | 2013.08.20 |
[iOS] iOS 화면 자동 회전 제어(Autorotate) 하기 (0) | 2013.06.17 |
[iOS] @property 값들의 차이 atomic, nonatomic 등등 (0) | 2013.05.20 |
[iOS] 스토리보드에서 코드를 이용한 View 전환에 관한 정리 (0) | 2013.04.11 |