0

As per our application specification, we need to schedule call for conference, while I scheduling I have been facing problem with Day light time difference.

This is the case: US day light time changes on Nov 3 this year, when I schedule a call for eg: 5 P.M on Nov 4, that time I am converting into GMT and send to server. If I convert that GMT time its giving 4 P.M Nov 4, instead of 5 P.M. Even though its correct only, however as per user perspective he scheduled for 5 P.M not 4 P.M, so how to handle this case.

I'm converting into GMT like ...

- (NSDateComponents*) convert_to_gmt_time : (NSDate*) date
{
    NSDate *localDate = date;
    NSTimeInterval timeZoneOffset = [[NSTimeZone defaultTimeZone] secondsFromGMT];
    NSTimeInterval gmtTimeInterval = [localDate timeIntervalSinceReferenceDate] - timeZoneOffset;
    NSDate *gmtDate = [NSDate dateWithTimeIntervalSinceReferenceDate:gmtTimeInterval];

    NSDateComponents *date_comp = [[NSCalendar currentCalendar] components: NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:gmtDate];

    return date_comp;
}

Edit:

This is how I'm coverting GMT time to local...

  NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    [gregorian setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"MMM dd, yyyy"];
NSDate *strt_date_loc = [gregorian dateFromComponents:start_date]; // start_date i'm filling after received from server as GMT.

    // Get date string.
    NSString *strt_date = [dateFormatter stringFromDate: strt_date_loc];

Any help is really appreciated. Thanx.

4

3 回答 3

1

一次又一次,然后……:您无法将日期转换为时区。您可以将日期表示转换为时区。这不会更改日期。不要试图比日期更棘手。

NSDate 的意外值

如果您有本地格式(即美国)的日期表示,只需使用本地日期格式化程序从它构建一个日期。然后将此日期发送到 $anywhere。

在回来的路上读取这个日期(不是它的表示之一)并使用本地格式化程序构建一个本地表示。

从不将日期的任何本地表示发送到任何地方。GMT 是本地代表。(G代表什么?)

于 2013-10-29T05:42:35.217 回答
1

NSDate 是一个绝对时间,而不是基于时区。所以基本上它就像在 GMT 描述的那样存储时间。

所以你只需要获取一个时区为 GMT 的日历,然后获取组件。

 NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
 [calendar setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];

 NSDateComponents *date_comp = [calendar components: NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:gmtDate];
于 2013-10-29T05:43:58.080 回答
0

有问题,

问题在于 GMT 转换,因为我确实考虑了与 GMT 的当前时差,而不是在转换日期有效的时差。

我就是这样固定的...

NSCalendar *cal = [NSCalendar currentCalendar];
[cal setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDateComponents *date_comp = [cal components: NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:localDate];
于 2013-11-04T07:02:25.163 回答