iOS 9 : 添加 GMT 时间值
iOS 9 : Add GMT Time Value
我在 google 上搜索了如何在 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 时间吗? (格林威治标准时间+5)
可以这样试试
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"GMT+5:30"];
最灵活的方法是使用timeZoneForSecondsFromGMT
。这比使用 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]];
}
它returns 2016-08-29T12:13(当GMT时间为2016-08-29T7:13 )
注:60*60*5
表示5 hours X 60 minutes X 60 seconds
如果您创建 NSDate
实例,那么它将 return 日期默认为 UTC or GMT
格式。
现在,当您通过任何日期格式化程序将此日期转换为字符串时,returned 字符串(日期)将处于本地时区(即设备的时区)。
您可以使用它的名称创建自定义时区以获取该时区的日期。
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(@"timezone : %@",timeZone);
将打印类似 Asia/Kolkata (IST) offset 19800
的内容。 19800
是偏移量,如果你将它除以 3600
那么你将得到与 gmt
的差异,例如 (+/- 5.30 etc)
Link for different timezone names
或者你可以得到像这样的时区,
NSTimeZone *timezone1 = [NSTimeZone timeZoneWithName:@"GMT+5:30"];
NSLog(@"timezone : %@",timezone1);
NSTimeZone *timeAone2 = [NSTimeZone timeZoneForSecondsFromGMT:60*60*5.5];
NSLog(@"timezone : %@",timeAone2);
我在 google 上搜索了如何在 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 时间吗? (格林威治标准时间+5)
可以这样试试
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"GMT+5:30"];
最灵活的方法是使用timeZoneForSecondsFromGMT
。这比使用 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]];
}
它returns 2016-08-29T12:13(当GMT时间为2016-08-29T7:13 )
注:60*60*5
表示5 hours X 60 minutes X 60 seconds
如果您创建 NSDate
实例,那么它将 return 日期默认为 UTC or GMT
格式。
现在,当您通过任何日期格式化程序将此日期转换为字符串时,returned 字符串(日期)将处于本地时区(即设备的时区)。
您可以使用它的名称创建自定义时区以获取该时区的日期。
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(@"timezone : %@",timeZone);
将打印类似 Asia/Kolkata (IST) offset 19800
的内容。 19800
是偏移量,如果你将它除以 3600
那么你将得到与 gmt
的差异,例如 (+/- 5.30 etc)
Link for different timezone names
或者你可以得到像这样的时区,
NSTimeZone *timezone1 = [NSTimeZone timeZoneWithName:@"GMT+5:30"];
NSLog(@"timezone : %@",timezone1);
NSTimeZone *timeAone2 = [NSTimeZone timeZoneForSecondsFromGMT:60*60*5.5];
NSLog(@"timezone : %@",timeAone2);