티스토리 뷰

//일수 구하기
    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]);


공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함