2013-05-25 5 views
0

datepicker가있는 두 textsfield를 빼는 방법을 알고 싶습니다. 결과를 레이블에 표시하십시오. 예 : (2013년 5월 25일) - (2013년 5월 17일) = 1 1 datepicker가있는 두 textsfield를 뺍니다.

NSDate *now = [NSDate date]; 
NSDate *then = self.datepicker.date; 
NSTimeInterval howLong = [now timeIntervalSinceDate:then]; 

NSUInteger w, d; 
w = (howLong/604800); 
d = 


NSString *str = [NSString stringWithFormat:@" x weeks, y days", w, d]; 
label1.text = str; 
+0

가, 다음, 다음 장치를 계속 예를 들어 월 나머지 –

+0

힌트를 계산한다. 변환 둘 다 1970 년부터 총 초 수로 나눕니다. –

답변

0

먼저 당신이 NSDateFormatter를 사용 NSDateNSString 변환해야합니다.

그런 다음 두 날짜 간 간격을 찾고 초를 입력 한 다음 필요에 따라 날짜를 분, 날짜로 변환 할 수 있습니다.

NSDateFormatter *df=[NSDateFormatter new]; 
[df setDateFormat:@"dd.MM.yyyy"]; 
NSDate *date1=[df dateFromString:@"25.05.2013"]; 
NSDate *date2=[df dateFromString:@"17.05.2013"]; 

NSInteger interval = (int)[date1 timeIntervalSinceDate: date2]; 

* 코드가 컴파일되지 않았습니다. NSDateComponents를 사용

0

시도 :

NSDateFormatter *form = [[NSDateFormatter alloc]init]; 
    [form setDateFormat:@"dd.MM.yyyy"]; 

    NSDate *dateA = [form dateFromString:@"17.05.2013"]; 
    NSDate *dateB = [form dateFromString:@"25.05.2013"]; 

    NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease]; 
    NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit 
               fromDate:dateA 
               toDate:dateB 
               options:0]; 
    strDiffyear = [NSString stringWithFormat:@"%i", components.year]; 
    strDiffmonth = [NSString stringWithFormat:@"%i",components.month]; 
    strDiffDays = [NSString stringWithFormat:@"%i",components.day]; 
lblDifference.text = [NSString stringWithFormat:@"%@ years %@ months %@ days",strDiffyear,strDiffmonth,strDiffDays]; 

당신은 날짜 차이 부문에 대한 많은 구성 요소를 가질 수 있습니다.

대답 어떤 쿼리, 무료 물어 경우 (:

이 시작 표시 할 가장 큰 장치, 예를 들면 년도에서 계산