2014-03-25 6 views
0

나는 [NSDate date]를 사용하여 현재 날짜를 얻고 있지만 앞으로 5 시간 30 mts를 반환하고있다. PM과 PM 후에 다른 시간으로 돌아갑니다. 내 요구 사항은 1 시간 동안 캐시에서 데이터를 삭제하려고합니다.NSDate는 5 시간 30 분을 더 돌려 준다

현재 시간 차이를 계산하려면 다음 코드를 사용하고 있습니다.

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; 
[dateFormatter setLocale:[NSLocale currentLocale]]; 

를 또는 어떤을하는 경우 : // storyDate는 캐시에 저장 한 ...

NSTimeInterval timeGapInSeconds =fabs([[NSDate date] timeIntervalSinceDate:storyDate]); 
NSInteger daysGap= timeGapInSeconds/86400; 
NSInteger hoursGap = timeGapInSeconds/3600; 
NSInteger mtsGap = timeGapInSeconds/60; 

NSLog(@"days gap %lu, hours gap is %lu",(long)daysGap,(long)hoursGap); 
NSLog(@"time gap in seconds %f",timeGapInSeconds); 
NSLog(@"time gap in mins %f",timeGapInSeconds/60); 
NSLog(@"current date:%@",[NSDate date]); 
+2

NSFT에서 날짜를 쓸 때 사용되는 GMT와 다른 로컬 시간대가 있기 때문에 – Volker

+4

캘린더 계산에 86400과 같은 상수를 사용하지 마십시오 (23 일 또는 25 시간이있는 요일 - 일광 절약 시간제라고 생각하십시오!). NSCalendar, NSDateComponents를 사용하십시오 ... –

+4

당신이 인도에 있기 때문입니다. –

답변

0

당신이 그것을 일반화 수 있도록하려는 경우 당신은 당신의 dateFormatter이를 추가 할 수있다 특정 국가에 대한 신청서는 다음을 사용하십시오.

NSLocale *usLoc = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]; 
[dateFormatter setLocale:usLoc]; 

희망이 있습니다.