2013-05-18 68 views
0

我想知道兩個日期(包括天數,小時,分鐘和秒)之間的差異。爲此,我使用下面的代碼,但它不工作。NSDateFormatter不工作設置setTimeZone

+ (void)UpdateTableViewCellWithNSTimer:(NSString *)gameTime :(UIView *)inputView{   
    NSDate *now = [NSDate date]; 
    NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init]; 
    [outputFormatter setLocale:[NSLocale systemLocale]]; 
    [outputFormatter setAMSymbol:@"AM"]; 
    [outputFormatter setPMSymbol:@"PM"]; 
    [outputFormatter setTimeZone:[NSTimeZone localTimeZone]]; 
    [outputFormatter setDateFormat:@"mm/dd/yyyy hh:mm a"]; 
    NSDate *gameDate = [outputFormatter dateFromString:gameTime]; 
    NSCalendar *gregorian = [[NSCalendar alloc]        initWithCalendarIdentifier:NSGregorianCalendar]; 
    NSUInteger unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit; 
    NSDateComponents *components = [gregorian components:unitFlags            fromDate:gameDate toDate:now options:0]; 
    NSInteger days = [components day]; 
    NSInteger hours = [components hour]; 
    NSInteger seconds = [components second]; 
    NSInteger minutes = [components minute]; 

    UITableView *dynamicTable = (UITableView *)[inputView viewWithTag:55]; 
    NSIndexPath *cellIndexPath = [NSIndexPath indexPathForRow:4 inSection:0]; 
    UITableViewCell *currentCell = [dynamicTable cellForRowAtIndexPath:cellIndexPath]; 
    NSString *challengeStartsIn=[NSString stringWithFormat:@"%01ddays:%02dhrs:%02dmins:%02dsec",abs(days),abs(hours), abs(minutes), abs(seconds)]; 
    NSString *Mystring =[NSString stringWithFormat:@"%@  %@", @"ChallengeStartsIn", challengeStartsIn]; 
    currentCell.textLabel.text = Mystring; 
} 
**Game and now dates on logs are:** 

2013-05-18 11:36:42.609 FPPresent[2213:5203] gametime value=05/19/2013 5:30 AM 
2013-05-18 11:36:42.610 FPPresent[2213:5203] now=2013-05-18 06:06:42 +0000 

使用outputformatter後:

gametime value =2013-01-19 00:00:00 +0000(It should be 2013-01-19 05:30:00 +0000) 
now=2013-05-18 06:10:07 +0000(should be : 2013-05-18 11:40:07 +0000) 

比賽日期和現在應該日期localTimeZone。但是,即使我嘗試設置

[outputFormatter setTimeZone:[NSTimeZone localTimeZone]]; 

我的數據格式顯示(沒錯5.30小時不同,我們的本地時區爲GMT + 5.30)

請讓我知道我錯了

+2

我累了一遍又一遍又一遍又一遍又一遍地看日期格式問題的...和 –

+0

http://developer.apple.com/library/ios/ipad/#qa /qa1480/_index.html – Wain

回答

2

[NSDate的日期]正在採取默認的時區爲GMT。所以,我無法設置我的當地時區。

雖然知道GMT之間的差別偏移到地方,我能得到兩個日期之間的差異成功地。

NSDate* sourceDate = [NSDate date]; 
    NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; 
    NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone]; 
    NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate]; 
    NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate]; 
    NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset; 
    NSDate* destinationDate = [[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate]; 
    NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init]; 
    [outputFormatter setAMSymbol:@"AM"]; 
    [outputFormatter setPMSymbol:@"PM"]; 
    [outputFormatter setLocale:[NSLocale currentLocale]]; 
    [outputFormatter setDateFormat:@"MM/dd/yyyy hh:mm a"]; 

NSDate *gameDate = [outputFormatter dateFromString:gameTime]; 
NSDate* gameDestinationDate = [[NSDate alloc] initWithTimeInterval:interval sinceDate:gameDate]; 
// NSLog(@"gameDate=%@",gameDate); 
NSCalendar *gregorian = [[NSCalendar alloc]     initWithCalendarIdentifier:NSGregorianCalendar]; 
NSUInteger unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit; 
NSDateComponents *components = [gregorian components:unitFlags       fromDate:destinationDate toDate:gameDestinationDate options:0]; 
NSInteger days = [components day]; 
NSInteger hours = [components hour]; 
NSInteger seconds = [components second]; 
NSInteger minutes = [components minute]; 

return ([NSString stringWithFormat:@"%01ddays:%02dhrs:%02dmins:%02dsec",abs(days),abs(hours), abs(minutes), abs(seconds)]); 
0

你與你加上時間差來輸出日期設置時區的輸出格式,並沿?因爲,有沒有需要補充的時間差爲設定時區兩個dateFormatters將自動時區之間的時間差的照顧。