2012-09-04 35 views
8

我找到了計算兩個日期之間的日差的代碼here
我寫一個方法:獲取特定時區中兩個NSDate日期之間的天數

-(NSInteger)daysWithinEraFromDate:(NSDate *) startDate toDate:(NSDate *) endDate 
{ 
    NSCalendar *gregorian = [[NSCalendar alloc] 
          initWithCalendarIdentifier:NSGregorianCalendar]; 
    NSInteger startDay=[gregorian ordinalityOfUnit:NSDayCalendarUnit 
              inUnit: NSEraCalendarUnit forDate:startDate]; 
    NSInteger endDay=[gregorian ordinalityOfUnit:NSDayCalendarUnit 
              inUnit: NSEraCalendarUnit forDate:endDate]; 
    return endDay-startDay; 
} 

這個方法有一個問題:它可以不考慮該時區的事情。即使我添加一行:

[gregorian setTimeZone:[NSTimeZone localTimeZone]]; 

我的測試代碼是這樣的:

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
    [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 
    NSString *strDate = @"2012-09-03 23:00:00"; 
    NSDate *dateStart = [dateFormat dateFromString:strDate]; 
    strDate = @"2012-09-04 01:00:00"; 
    NSDate *dateEnd = [dateFormat dateFromString:strDate]; 
    NSLog(@"Days difference between %@ and %@ is: %d days",[dateFormat stringFromDate:dateStart],[dateFormat stringFromDate:dateEnd],[self daysWithinEraFromDate:dateStart toDate:dateEnd]); 

結果是:2012-09-03 23:00

天差:00和2012年9月4日01:00:00是:0天

我想midnig數量拿到1一天的結果這兩個日期之間的時間。我的時區是格林威治標準時間+8。但是這個計算是基於GMT的,所以我得到了錯誤的天數。無論如何要解決這個問題嗎?謝謝。


Scott Lemmon的方法可以解決我的問題。我重寫我的代碼是這樣的:

-(NSInteger)daysWithinEraFromDate:(NSDate *) startDate toDate:(NSDate *) endDate 
{ 
    NSCalendar *gregorian = [[NSCalendar alloc] 
          initWithCalendarIdentifier:NSGregorianCalendar]; 
    [gregorian setTimeZone:[NSTimeZone localTimeZone]]; 
    NSDate *newDate1 = [startDate dateByAddingTimeInterval:[[NSTimeZone localTimeZone] secondsFromGMT]]; 
    NSDate *newDate2 = [endDate dateByAddingTimeInterval:[[NSTimeZone localTimeZone] secondsFromGMT]]; 

    NSInteger startDay=[gregorian ordinalityOfUnit:NSDayCalendarUnit 
              inUnit: NSEraCalendarUnit forDate:newDate1]; 
    NSInteger endDay=[gregorian ordinalityOfUnit:NSDayCalendarUnit 
              inUnit: NSEraCalendarUnit forDate:newDate2]; 
    return endDay-startDay; 
} 

回答

5

如果時區偏移不起作用,那麼只需手動添加或減去它即可?

你的情況NSDate *newDate = [oldDate dateByAddingTimeInterval:(-8 * 60 * 60)];減去你的+8小時。

或者,如果你想找到GMT偏移量自動,以及那麼這將僅僅是NSDate *newDate = [oldDate dateByAddingTimeInterval:(-[[NSTimeZone localTimeZone] secondsFromGMT])

另一種思考: 一個也許更容易的解決辦法是隻完全忽略了時間的信息。只要將這兩個日期的數字設置爲相同的任意數字,那麼只要日期來自同一時區,無論GMT偏移量如何,您都將始終獲得正確的夜間數字。

+0

嗨,謝謝你的回答。我只是想着無視時間信息。我在我的代碼中完成了這項工作。但你的第一個解決方案非常棒!我認爲這是解決這個問題的更精確的方法。我已經在我的代碼中嘗試過了,我會向他們展示它們。 – Vigor

3

你真正想要的是什麼NSDate方法timeIntervalSinceDate:,並採取結果,並且如果它是大於0小於86400(以秒爲單位,每天的數量),這是一個天。否則,將結果除以86400,您將得到天數。

你現在有你的代碼的方式,這兩天之間只有2個小時,這就是爲什麼你看到0而不是1的結果。

編輯 - 並確定午夜已經發生了,讓我們試試這個功能,我只寫了我的頭頂部:

- (NSDate *) getMidnightDateFromDate: (NSDate *) originalDate 
{ 
    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSIntegerMax fromDate:originalDate]; 
    [components setHour:0]; 
    [components setMinute:0]; 
    [components setSecond:0]; 
    NSDate *midnight = [[NSCalendar currentCalendar] dateFromComponents:components]; 
    return(midnight); 
} 

- (BOOL) howManyDaysDifferenceBetween: startDate and: endDate 
{ 
    NSDate * firstMidnight = [self getMidnightDateFromDate: startDate]; 
    NSDate * secondMidnight = [self getMidnightDateFromDate: endDate]; 
    NSTimeInterval timeBetween = [firstMidnight timeIntervalSinceDate: secondMidnight]; 

    NSInteger numberOfDays = (timeBetween/86400); 

    return(numberOfDays); 
} 

這我立足關Dave Delong's answer to this question。沒有保證我的代碼可以工作(我沒有測試它),但我認爲這個概念是合理的。

+0

謝謝你的回答。我想要確定兩個NSDate實例是否在兩個日期之間的midnights數量不同的日期。在你的方法下,我仍然會得到0天。但2012-09-03和2012-09-04是在不同的日期,即使間隔只有2小時。如果我將第二個日期更改爲2012-09-04 08:00:00,我將根據我的方法得到1天結果。我認爲這是因爲時區。 – Vigor

相關問題