2010-03-17 5 views
3

두 개의 날짜가 있습니다 (MM/dd/yyyy hh : mm : ss : SS). 두 날짜 모두 (stringFromDate) 메서드를 사용하여 두 날짜를 문자열로 변환했습니다. 그러나 나는 그들 사이의 차이점을 얻지 못하고 내 콘솔에 보여줄 수 없었다. 내가 어떻게해야하는지 생각 나게 해줘? 감사합니다.두 날짜의 차이점을 얻으려면 어떻게해야합니까?

답변

3

NSDate *today = [NSDate date]; 

NSTimeInterval dateTime; 


if ([visitDate isEqualToDate:today]) //visitDate is a NSDate 

{ 

NSLog (@"Dates are equal"); 

} 

dateTime = ([visitDate timeIntervalSinceDate:today]/86400); 

if(dateTime < 0) //Check if visit date is a past date, dateTime returns - val 

{ 

NSLog (@"Past Date"); 

} 

else 

{ 
NSLog (@"Future Date"); 

} 
+0

나는 퍼팅 추천하는이 같은 일정에 매직 넘버 (86400) : 'const를 CGFloat kSecondsPerDay = 60 * 60 * 24;'... – zekel

0

은 일반적으로 I (1970년 1월 1일 같은 일부 출발 epoch 때문에 보통 일) 일 델타 일 평면으로 일/년 값 변환 처리 계산을 참조.

이 문제를 해결하기 위해 매달 시작되는 연도를 일별로 만드는 것이 도움이된다는 것을 발견했습니다. 최근에 이것을 사용했던 수업이 있습니다.

namespace { 
    // Helper class for figuring out things like day of year 
    class month_database { 
    public: 
     month_database() { 

      days_into_year[0] = 0; 
      for (int i=0; i<11; i++) { 
       days_into_year[i+1] = days_into_year[i] + days_in_month[i]; 
      } 
     }; 

     // Return the start day of the year for the given month (January = month 1). 
     int start_day (int month, int year) const { 

      // Account for leap years. Actually, this doesn't get the year 1900 or 2100 right, 
      // but should be good enough for a while. 
      if ((year % 4) == 0 && month > 2) { 
       return days_into_year[month-1] + 1; 
      } else { 
       return days_into_year[month-1]; 
      } 
     } 
    private: 
     static int const days_in_month[12]; 

     // # of days into the year the previous month ends 
     int days_into_year[12]; 
    }; 
    // 30 days has September, April, June, and November... 
    int const month_database::days_in_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 

    month_database month; 
} 

당신이 start_day 방법에서 볼 수 있듯이

은, 당신이 레슬링하려고하는 주요 문제는 많은 도약의 일이 당신의 범위에 포함하는 방법이다. 우리 시대에서 사용 된 계산은 충분합니다. 연도에 윤년이 포함되는 실제 규칙은 discussed here입니다. 그레고리력의

2월 29일

, 가장 널리 사용되는 오늘, 4로 나누어 년에 한 번 매 4 년에만 발생 날짜 , 같은 1976, 1996, 2000, 2004입니다 , 2008, 2012 또는 2016 ( 세기는 400으로 나눌 수 없으며, 예 : 1900과 같은 ).

2

날짜를 날짜로 유지하고 차이를 가져온 다음 차이점을 인쇄하십시오. 그레고리을 가정 docs on NSCalendar과에서

은 NSCalendar입니다 : 당신은 그냥 일의 차이를 원한다면

NSDate *startDate = ...; 

NSDate *endDate = ...; 

unsigned int unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit; 

NSDateComponents *comps = [gregorian components:unitFlags fromDate:startDate toDate:endDate options:0]; 

int months = [comps month]; 

int days = [comps day]; 
0

, 당신은이 작업을 수행 할 수 있습니다. (미 히어의 메타의 답변에 따라.)

const NSTimeInterval kSecondsPerDay = 60 * 60 * 24; 
- (NSInteger)daysUntilDate:(NSDate *)anotherDate { 
    NSTimeInterval secondsUntilExpired = [self timeIntervalSinceDate:anotherDate]; 
    NSTimeInterval days = secondsUntilExpired/kSecondsPerDay; 
    return (NSInteger)days; 
} 
+0

(당신은 추가해야이 NSDate의 카테고리 메소드로 사용). – zekel

관련 문제