D-day 구하기
오늘부터 특정일까지 D-day 구하는 소스입니다. 간단해요. 한번 해보시면 느낌이 오실겁니다.
NSDateFormatter *format1 = [[NSDateFormatter alloc] init];
[format1 setDateFormat:@"yyyyMMdd"];
NSDate *DateOfBirth=[format1 dateFromString:@"20131220"];
NSDate *currentTime = [NSDate date];
int to = [[format1 stringFromDate:currentTime] intValue];
int birth = [[format1 stringFromDate:DateOfBirth] intValue];
if (to > birth) {
titleOfAlips = @"종료";
} else if (to == birth) {
titleOfAlips = @"D-day";
}
else {
NSDateComponents *dcom = [[NSCalendar currentCalendar] components:NSDayCalendarUnit fromDate:DateOfBirth toDate:currentTime options:0];
titleOfAlips = [NSString stringWithFormat:@"D%d", [dcom day]-1];
}
currentTime은 현재 날짜입니다.
현재보다 특정일이 미래이면 값이 마이너스 값으로 나와요. NSDateComponents에서 fromDate와 toDate에 따라 +, - 값이 결정되네요.
예를 들어 오늘이 20131121일인데 특정일을 내일 (20131122)로 잡으면 -2가 됩니다. 하루 남았는데 -2값이 들어오네요.. 그래서 위에 보시면 [dcom day]-1 을 해주었습니다.
음 US 기준으로 계산을 해서 그런가..
'IT > iOS' 카테고리의 다른 글
UITableView selected background color (0) | 2014.02.04 |
---|---|
iOS6 특정뷰 회전 처리 (0) | 2014.02.04 |
iPhone emoji encoding decoding (0) | 2014.01.28 |
CopyPNGfile error (0) | 2013.12.05 |
NSMutableArray Sort (0) | 2013.11.07 |
string 정규식 사용하여 html tag 삭제하기 (0) | 2013.10.28 |
데이타 인코딩 (0) | 2013.10.15 |
앱스토어 리뷰 페이지 이동 (0) | 2013.08.19 |