날짜와 시간
NSDate 생성
지금 시각에서 24시간 이후의 시각 생성
지금 갖고 있는 NSDate 객체 기준으로 일정 시간 이전의 시각 구하기
두 개의 시각이 일치하는지 확인
두 개의 시각 중 먼저인 것과 나중인 시각 찾기
두 시각 사이의 간격을 초 단위로 계산
현재 시각을 기준으로 특정 시점까지의 시간 간격(초)
2010년 6월 1일 나타내는 NSDate 객체
기존의 NSDate 인스턴스에서 날짜와 월, 년도 등을 뽑기
초 단위의 범위를 사용하지 않고 내일 날짜 알아보기(NSCalendar 클래스 활용)
NSDateFormatter 를 날짜를 문자열로 표현
현재 시각을 문자열로 받아오기
날짜나 시각 표현 방법을 직접 지정 가능
NSDate 생성
1 | NSDate *myDate = [NSDate date]; |
지금 시각에서 24시간 이후의 시각 생성
1 2 3 | NSTimeInterval secondsPerDay = 24 * 60 * 60 ; NSDate *tomorrow = [NSDate dateWirhTimeIntervalSinceNow:secondsPerDay]; |
지금 갖고 있는 NSDate 객체 기준으로 일정 시간 이전의 시각 구하기
1 2 3 | NSTimeInterval secondsPerDay = 24 * 60 * 60 ; NSDate *now = [NSDate date]; NSDate *yesterday = [now addTimeInterval:-secondsPerDay]; |
두 개의 시각이 일치하는지 확인
1 | BOOL sameDate = [date1 isEqualToDate:date2]; |
두 개의 시각 중 먼저인 것과 나중인 시각 찾기
1 2 | NSDate *earlierDate = [date1 earlierDate:date2]; NSDate *laterDate = [date1 laterDate:date2]; |
두 시각 사이의 간격을 초 단위로 계산
1 2 | NStimeInterval secondsBetweenDates = [date2 timeIntervalSinceDate:date1]; |
현재 시각을 기준으로 특정 시점까지의 시간 간격(초)
1 2 | NSTimeInterval secondsUntilTomorrow = [tomorrow timeIntervalSinceNow]; |
2010년 6월 1일 나타내는 NSDate 객체
1 2 3 4 5 6 7 | NSDateComponents *comp = [[NSDateComponents alloc] init]; [comp setMonth: 06 ]; [comp setDay: 01 ]; [comp setYear: 2010 ]; NSCalender *myCal = [[NSCalendar alloc] initWithCalenderIdentifier:NSGregorianCalendar]; NSDate *myDate = [myCal dateFromComponents:comp]; |
기존의 NSDate 인스턴스에서 날짜와 월, 년도 등을 뽑기
1 2 3 4 5 6 7 8 | unsigned units = NSMonthCalendarUnit | NSDayCalendarUnit | NSYearCalendatUnit; NSDate *now = [NSDate date]; NSCalendar *myCal = [[NSCalendat alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *comp = [myCal components:units fromDate:now]; NSInteger month = [comp month]; NSInteger day = [comp day]; NSInteger year = [comp year]; |
초 단위의 범위를 사용하지 않고 내일 날짜 알아보기(NSCalendar 클래스 활용)
1 2 3 4 5 6 7 | NSDate *now = [NSDate date]; NSDateComponents *comp = [[NSDateComponents alloc] init]; [comp setDay: 01 ]; NSCalendat *myCal = [[NSCalendat alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDate *tomorrow = [myCal dateByAddingComponents:comp toDate:now options: 0 ]; |
NSDateFormatter 를 날짜를 문자열로 표현
1 2 3 4 | NSDate *now = [NSDate date]; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateStyle:NSDateFormatterMediumStyle]; NSString *friendlyDate = [formatter stringFromDate:now]; |
현재 시각을 문자열로 받아오기
1 2 3 4 | NSDate *now = [NSDate date]; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setTimeStyle:NSDateFormatterMediumStyle]; NSString *friendlyTime = [formatter stringFromDate:now]; |
날짜나 시각 표현 방법을 직접 지정 가능
1 2 3 4 | NSDate *now = [NSDate date]; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@ "yyyy-mm-dd" ]; NSString *friendlyDate = [formatter setingFromDate:now]; |
'프로그래밍 > Object-C' 카테고리의 다른 글
NSArray (0) | 2011.06.21 |
---|---|
NSDateFormatter (0) | 2011.06.20 |
NSString 클래스 (0) | 2011.06.20 |
전환 시 애니메이션 추가 (0) | 2011.05.01 |
멀티뷰 만들기 (0) | 2011.05.01 |