iOS 9:添加GMT时间值

iOS 9:添加GMT时间值,ios,objective-c,iphone,nsdate,nsdateformatter,Ios,Objective C,Iphone,Nsdate,Nsdateformatter,我在谷歌上搜索了如何在Objective-C中获取GMT时间,我得到了以下信息: + (NSString *)GetGMTTime{ NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm"; NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; [da

我在谷歌上搜索了如何在Objective-C中获取GMT时间,我得到了以下信息:

+ (NSString *)GetGMTTime{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm";

NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
[dateFormatter setTimeZone:gmt];

return [dateFormatter stringFromDate:[NSDate date]];}
有没有办法添加或设置GMT时间?(GMT+5)

可以这样尝试

 NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"GMT+5:30"];
我可以这样试试

 NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"GMT+5:30"];

最灵活的方法是使用。这比使用GMT+X这样的字符串更能防止输入错误

下面是一个非常有效的例子:

+ (NSString *)GetGMTTime{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm";

    NSTimeZone *gmt = [NSTimeZone timeZoneForSecondsFromGMT:(60*60*5)];
    [dateFormatter setTimeZone:gmt];

    return [dateFormatter stringFromDate:[NSDate date]];

}
它返回2016-08-29T12:13(格林尼治时间为2016-08-29T7:13时)


注:
60*60*5
表示
5小时X 60分钟X 60秒

最灵活的方法是使用。这比使用GMT+X这样的字符串更能防止输入错误

下面是一个非常有效的例子:

+ (NSString *)GetGMTTime{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm";

    NSTimeZone *gmt = [NSTimeZone timeZoneForSecondsFromGMT:(60*60*5)];
    [dateFormatter setTimeZone:gmt];

    return [dateFormatter stringFromDate:[NSDate date]];

}
它返回2016-08-29T12:13(格林尼治时间为2016-08-29T7:13时)


注意:
60*60*5
表示
5小时X 60分钟X 60秒

如果创建
NSDate
实例,则默认情况下它将以
UTC或GMT
格式返回日期

现在,当您通过任何日期格式化程序将此日期转换为字符串时,返回的字符串(日期)将位于本地时区(即设备的时区)

您可以使用其名称创建自定义时区,以获取该时区中的日期

  NSDate *date = [NSDate date]; //give current date in UTC or GMT
NSLog(@"current date in gmt : %@",date);

NSDateFormatter *df = [[NSDateFormatter alloc]init];
[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

NSString *dateInStrFormat = [df stringFromDate:date];  // this date(string format) will be in current timezone that was set in your device and

NSLog(@"current date in local or device's default timezone : %@",dateInStrFormat);

NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Asia/Kolkata"];

NSLog(@"timezone : %@",timeZone);

[df setTimeZone:timeZone];

NSString *dateInCustomTimeZone = [df stringFromDate:date]; // this will return date in Asia/Kolkata's timezone

NSLog(@"date in custom timezone : %@",dateInCustomTimeZone);
NSLog(@“时区:%@”,时区)
将打印类似于,
亚洲/加尔各答(IST)偏移量19800
19800
是偏移量,如果您用
3600
除以它,您将得到与
gmt
类似的
(+/-5.30等)

或者你可以有时区,比如

  NSTimeZone *timezone1 = [NSTimeZone timeZoneWithName:@"GMT+5:30"];
NSLog(@"timezone : %@",timezone1);

NSTimeZone *timeAone2 = [NSTimeZone timeZoneForSecondsFromGMT:60*60*5.5];
NSLog(@"timezone : %@",timeAone2);

如果创建
NSDate
实例,则默认情况下,它将以
UTC或GMT
格式返回日期

现在,当您通过任何日期格式化程序将此日期转换为字符串时,返回的字符串(日期)将位于本地时区(即设备的时区)

您可以使用其名称创建自定义时区,以获取该时区中的日期

  NSDate *date = [NSDate date]; //give current date in UTC or GMT
NSLog(@"current date in gmt : %@",date);

NSDateFormatter *df = [[NSDateFormatter alloc]init];
[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

NSString *dateInStrFormat = [df stringFromDate:date];  // this date(string format) will be in current timezone that was set in your device and

NSLog(@"current date in local or device's default timezone : %@",dateInStrFormat);

NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Asia/Kolkata"];

NSLog(@"timezone : %@",timeZone);

[df setTimeZone:timeZone];

NSString *dateInCustomTimeZone = [df stringFromDate:date]; // this will return date in Asia/Kolkata's timezone

NSLog(@"date in custom timezone : %@",dateInCustomTimeZone);
NSLog(@“时区:%@”,时区)
将打印类似于,
亚洲/加尔各答(IST)偏移量19800
19800
是偏移量,如果您用
3600
除以它,您将得到与
gmt
类似的
(+/-5.30等)

或者你可以有时区,比如

  NSTimeZone *timezone1 = [NSTimeZone timeZoneWithName:@"GMT+5:30"];
NSLog(@"timezone : %@",timezone1);

NSTimeZone *timeAone2 = [NSTimeZone timeZoneForSecondsFromGMT:60*60*5.5];
NSLog(@"timezone : %@",timeAone2);

您需要GMT+5:30或GMT+5您需要GMT+5:30或GMT+5