Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.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 从日期字符串获取时区偏移量_Objective C_Nsdate_Nsdateformatter - Fatal编程技术网

Objective c 从日期字符串获取时区偏移量

Objective c 从日期字符串获取时区偏移量,objective-c,nsdate,nsdateformatter,Objective C,Nsdate,Nsdateformatter,我将以下日期作为字符串:2015-04-18T08:35:00.000+03:00,我希望以秒为单位获得GMT的偏移量,即10800 我可以用“+”字符分割日期字符串,得到一个字符串xy:ab,然后用公式xy*3600+a*60+b计算偏移量 是否有更好的方法来实现我的需求,例如使用NSDate和NSDateFormatter?我最终使用了以下代码: NSString *dateStringWithTZ = @"2015-04-18T08:35:00.000+03:00"; /

我将以下日期作为字符串:2015-04-18T08:35:00.000+03:00,我希望以秒为单位获得GMT的偏移量,即10800

我可以用“+”字符分割日期字符串,得到一个字符串xy:ab,然后用公式xy*3600+a*60+b计算偏移量


是否有更好的方法来实现我的需求,例如使用NSDate和NSDateFormatter?

我最终使用了以下代码:

    NSString *dateStringWithTZ = @"2015-04-18T08:35:00.000+03:00";

    // Create date object using the timezone from the input string.
    NSDateFormatter *dateFormatterWithTZ = [[NSDateFormatter alloc] init];
    dateFormatterWithTZ.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss.SSSZ";
    NSDate *dateWithTZ = [dateFormatterWithTZ dateFromString:dateStringWithTZ];

    // Cut off the timezone and create date object using GMT timezone.
    NSString *dateStringWithoutTZ = [dateStringWithTZ substringWithRange:NSMakeRange(0, 23)];
    NSDateFormatter *dateFormatterWithoutTZ = [[NSDateFormatter alloc] init];
    dateFormatterWithoutTZ.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss.SSS";
    dateFormatterWithoutTZ.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
    NSDate *dateWithoutTZ = [dateFormatterWithoutTZ dateFromString:dateStringWithoutTZ];

    // Calculate the difference.
    NSInteger difference = [dateWithoutTZ timeIntervalSinceReferenceDate] - [dateWithTZ timeIntervalSinceReferenceDate];
    return [NSNumber numberWithInteger:difference];

使用
NSDateFormatter
。这是一个很好的观点,但问题是我不知道如何使用。我可以创建一个NSDateFormatter实例并设置日期格式yyyy-MM-dd'T'HH:MM:ss.SSSZ。但是我怎样才能得到我想要的号码呢?