NSDateFormatter 无法设置 setTimeZone

Posted

技术标签:

【中文标题】NSDateFormatter 无法设置 setTimeZone【英文标题】:NSDateFormatter not working to set setTimeZone 【发布时间】:2013-05-18 06:16:57 【问题描述】:

我想知道两个日期之间的区别(包括天、小时、分钟和秒)。为此,我使用下面的代码,但它不起作用。

+ (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

使用输出格式化程序后

 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)

请告诉我哪里出错了

【问题讨论】:

我厌倦了一遍又一遍地看到一个日期格式问题,一遍又一遍…… developer.apple.com/library/ios/ipad/#qa/qa1480/_index.html 【参考方案1】:

[NSDate date] 将默认时区设为 GMT。所以,我无法将我的本地时区设置为迄今为止。

虽然知道 GMT OFFset 与 local 之间的差异,但我能够成功获得两个日期之间的差异。

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)]);

【讨论】:

【参考方案2】:

您是否正在为输出格式化程序设置时区,并且您是否正在将时差添加到输出日期?因为,不需要添加时差,因为为两个 dateFormatter 设置时区会自动处理时区之间的时差。

【讨论】:

以上是关于NSDateFormatter 无法设置 setTimeZone的主要内容,如果未能解决你的问题,请参考以下文章

NSDateFormatter 无法识别日期

NSDateFormatter 无法解析德语月份 March/June/Jul 的三个字母缩写

NSDateFormatter 无法解析德语月份 March/June/Jul 的三个字母缩写

NSDateFormatter 将日期从 twitter-timeline 设置为 (null)

Foundation框架—时间输出格式NSDateFormatter

将事件添加到日历时,NSDateFormatter 未正确格式化日期