A colossal Dreamer: GR鐵塔-天生我材

[아이폰:오브젝티브C] 날짜와 캘린더 그리고, 날짜조각 본문

Development/아이폰

[아이폰:오브젝티브C] 날짜와 캘린더 그리고, 날짜조각

江多林 2010. 1. 28. 09:54

아이폰OS에서 날짜와 연관된 클래스는 3개이고,
각각의 역할이 정해져 있다.

시스템수준의 날짜: NSDate
달력: NSCalendar
날짜조각: NSDateComponents
지역시간: NSTimeZone

NSDate 는 시스템간에 통용될 수 있는 오늘과 같은 하나의 시점을 표현하는 클래스이고,
NSDateComponents 는 인간이 알아먹을 수 있는 날짜이다.
NSTimeZone 은 NSCalendar에서만 사용이 가능한 것으로 UTC 기준의 시간을 지역시간으로 적용할 수 있다.

[양력 2010년 1월 28일/ 음력 2009년 12월 14일]
위의 두 날짜의 인간이 알아먹을 수 있는 표현로는 다르지만,
실제로는 하나의 날짜를 의미한다.
이제 그 사용예를 보자.


Getting a date’s components

NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc]
                         initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *weekdayComponents =
                    [gregorian components:(NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:today];
NSInteger day = [weekdayComponents day];
NSInteger weekday = [weekdayComponents weekday];

 


Creating a Date from Components

NSDateComponents *components = [[NSDateComponents alloc] init];
[components setWeekday:2]; // Monday
[components setWeekdayOrdinal:1]; // The first day in the month
[components setMonth:5]; // May
[components setYear:2008];
NSCalendar *gregorian = [[NSCalendar alloc]
                         initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *date = [gregorian dateFromComponents:components];


참조: 애플개발자사이트