NSDateFormatter无法设置setTimeZone [英] NSDateFormatter not working to set setTimeZone

查看:117
本文介绍了NSDateFormatter无法设置setTimeZone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to know the difference between two dates (Including days,hours,minutes and seconds). For that I am using below code, but It's not working.

+ (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中。但是,即使我尝试设置

The game date and now date should in localTimeZone. But, even I try to set

[outputFormatter setTimeZone:[NSTimeZone localTimeZone]];

我的数据格式显示(正好5.30小时不同,我们的当地时区是GMT + 5.30)

My data format is showing (Exactly 5.30 hours different, our local time zone is GMT+5.30)

请告诉我出错的地方

推荐答案

[NSDate日期]将默认时区视为GMT。所以,我无法设置我当地的时区到目前为止。

[NSDate date] is taking default timezone as GMT. so, I am unable to set my local timezone to date.

在了解GMT OFFset与本地之间的区别的同时,我能够成功地获得两个日期之间的差异。

While knowing the difference between GMT OFFset to local , I am able to get the difference between two dates sucessfully.

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

这篇关于NSDateFormatter无法设置setTimeZone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆