나름 프로그래밍?/ Objective-C
날짜 다루기
-Dong-
2011. 6. 16. 12:10
//일수 구하기
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comp = [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit) fromDate:self.viewDate];
[comp setDay:0];
[comp setMonth:comp.month+1];
int days = [[gregorian components:NSDayCalendarUnit fromDate:[gregorian dateFromComponents:comp]] day];
NSLog(@"day = %d", days);
[gregorian release];
//형식에 맞게 출력
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSString *compareDate = [dateFormatter stringFromDate:self.viewDate];
NSLog(@"compareDate = %@", compareDate);
addTimeInterval 이 deprecate 되어서 -> - (id)dateByAddingTimeInterval:(NSTimeInterval)seconds
//한달 계산
NSTimeInterval newValue = 0;
newValue = 60*60*24*days; // 한달계산용
self.viewDate = (NSDate *)[self.viewDate addTimeInterval:-newValue];
//하루계산
NSTimeInterval newValue = 0;
newValue = 60*60*24; // 하루계산용
self.viewDate = (NSDate *)[self.viewDate addTimeInterval:-newValue];
//날짜 문자열로 할당
NSDate *date = [[NSDate alloc] initWithString:@"2008-05-09
19:47:47 +0000"];
NSLog(@"date is %@", date);
//날짜 문자열로 할당(Warning 없이)
NSString *dateString = @"25-Dec-10";
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
dateFormatter.dateFormat = @"dd-MMM-yy";
NSDate *future = [dateFormatter dateFromString:dateString];
//날짜 차이
NSDateComponents *dateComp = [[NSCalendar currentCalendar]
components :NSHourCalendarUnit <- minute, hour, day...
fromDate : [NSDate date]
toDate : date
options:0];
NSLog(@"Hour, %d", [dateComp hour]);
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comp = [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit) fromDate:self.viewDate];
[comp setDay:0];
[comp setMonth:comp.month+1];
int days = [[gregorian components:NSDayCalendarUnit fromDate:[gregorian dateFromComponents:comp]] day];
NSLog(@"day = %d", days);
[gregorian release];
//형식에 맞게 출력
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSString *compareDate = [dateFormatter stringFromDate:self.viewDate];
NSLog(@"compareDate = %@", compareDate);
addTimeInterval 이 deprecate 되어서 -> - (id)dateByAddingTimeInterval:(NSTimeInterval)seconds
//한달 계산
NSTimeInterval newValue = 0;
newValue = 60*60*24*days; // 한달계산용
self.viewDate = (NSDate *)[self.viewDate addTimeInterval:-newValue];
//하루계산
NSTimeInterval newValue = 0;
newValue = 60*60*24; // 하루계산용
self.viewDate = (NSDate *)[self.viewDate addTimeInterval:-newValue];
//날짜 문자열로 할당
NSDate *date = [[NSDate alloc] initWithString:@"2008-05-09
19:47:47 +0000"];
NSLog(@"date is %@", date);
//날짜 문자열로 할당(Warning 없이)
NSString *dateString = @"25-Dec-10";
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
dateFormatter.dateFormat = @"dd-MMM-yy";
NSDate *future = [dateFormatter dateFromString:dateString];
//날짜 차이
NSDateComponents *dateComp = [[NSCalendar currentCalendar]
components :NSHourCalendarUnit <- minute, hour, day...
fromDate : [NSDate date]
toDate : date
options:0];
NSLog(@"Hour, %d", [dateComp hour]);