Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c NSDATE组件不断更改GMT?_Objective C - Fatal编程技术网

Objective c NSDATE组件不断更改GMT?

Objective c NSDATE组件不断更改GMT?,objective-c,Objective C,这可能是基本的,但我找不到类似的问题来清楚地理解这一点 我有一个日期是这样的: 2015-07-19 12:00:00 +0000 然后,我会选择这个日期,并尝试通过以下方式获得时间: NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; NSDateComponents *components = [gregoria

这可能是基本的,但我找不到类似的问题来清楚地理解这一点

我有一个日期是这样的:

2015-07-19 12:00:00 +0000
然后,我会选择这个日期,并尝试通过以下方式获得时间:

NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

    NSDateComponents *components = [gregorianCalendar components:NSCalendarUnitDay | NSCalendarUnitHour |NSCalendarUnitMinute | NSCalendarUnitMonth | NSCalendarUnitYear| NSCalendarUnitWeekday fromDate:tdate];
    long hour=[components hour];
      NSLog(@"-- %ld ",hour);   //gives 15
然后我得到的小时数是15,而不是12作为原始日期。 3个小时的不同之处在于我猜我的GMT+3。 我怎样才能得到原来的12小时

加上这个

[components setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
没有帮助

当我使用components类创建原始日期时,我会按照稍后再次执行的方式执行,因此GMT时间不会有任何更改

要创建原始日期,我需要执行以下操作:

 NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay |NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitWeekday | NSCalendarUnitHour | NSCalendarUnitMinute fromDate:new];

        [components setHour:12];
那么,为什么当你以完全相同的方式提取它时,却得不到相同的结果呢


谢谢。

当使用
NSCalender
nsdatecomponent
时,它将始终将日期转换为您的系统日期。如果希望具有该日期不变量,同时使用DateComponent访问组件,则需要删除SystemData与该日期系统之间的时间间隔(在您的情况下为GMT)


我不认为是这样,因为他应该在约会结束时给我看+0003,让我知道他加了3个小时。他所做的是实际更改日期,显示不同的小时,但不将GMT符号更改为+0003。NSDate本身是不可变的,但使用NSCalender+NSDateComponent对其组成部分的解释取决于您所在的区域。但我使用相同的日历创建日期并读取日期,为什么他会显示不同的值?这并不是说用完全相同的组件创建一个特定的日期,然后尝试读取它并得到不同的结果。这就像定义一个整数,然后打印它并获得另一个结果。您的日期包括对特定时区+000x的引用,否则它将是系统时区
NSTimeZone* currentTimeZone = [NSTimeZone localTimeZone];
NSTimeInterval timeDiff = [currentTimeZone secondsFromGMTForDate:aGMTDate];
NSDate *gmtDate = [[NSDate alloc] initWithTimeInterval:(-timeDiff) sinceDate:[NSDate date]];