2012-04-26 52 views
2

我使用一个RSS解析器列出时间错误。东部标准时间显示比RSS事件的实际时间晚1小时。所以,我想弄清楚如何在代码中调整它,以便在TableView的cellForIndexAtPathRow方法中显示正确的时间。这里是我使用的代码:iPhone调整NSDate的时间

NSDateFormatter * dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; 
[dateFormatter setTimeStyle:NSDateFormatterMediumStyle]; 
[dateFormatter setDateStyle:NSDateFormatterMediumStyle]; 
NSString *articleDateString = [dateFormatter stringFromDate:entry.articleDate]; 

的entry.articleDate来自RSS解析器早些时候返回:

NSString *articleDateString = [item valueForChild:@"updated"];   
    NSDate *articleDate = [NSDate dateFromInternetDateTimeString:articleDateString formatHint:DateFormatHintRFC3339]; 

你认为什么是采取的HH部分的最佳方式只有字符串,并从它减去1?

+0

NSTimeInterval secondsforOneHour = 60 * 60; NSDate * dateoneHourAhead = [articleDate dateByAddingTimeInterval:secondsforOneHour]; – Bala 2012-04-26 15:09:38

+2

加入'[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@“neededTimezone”]]'你应该很好。 – Eimantas 2012-04-26 15:20:04

+0

Eimantas答案效果最好。由于中央时间正确,我只需将时区设置为美国/芝加哥,并且无论用户身在何处,它都会显示正确的时间。谢谢! – user717452 2012-04-26 15:40:46

回答

0

你可以简单地从解析日期减去一个小时,像这样:

NSDateFormatter * dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; 
[dateFormatter setTimeStyle:NSDateFormatterMediumStyle]; 
[dateFormatter setDateStyle:NSDateFormatterMediumStyle]; 
NSDate *correctedDate = [entry.articleDate addTimeInterval:-3600*1]; 
NSString *articleDateString = [dateFormatter stringFromDate:correctedDate]; 

不过,我认为真正的问题出在你的RSS解析器。我敢打赌,它正在转换到您当地的时区。但是,没有看到实际的价值,我不能确定。