我已经搜索了很多关于Google的这个问题,但是找不到合适的解决方案。所有结果以日期和时间格式提供时区。
我想知道如何只获取GMT格式的设备时区。就像,我只想要这个
+5:30
作为TimeZone。我怎样才能实现这一目标?
我试过这个
NSTimeZone* currentTimeZone = [NSTimeZone localTimeZone];
但它会在kolkata / Delhi给出结果。我也尝试了其他答案,但没有找到解决方案。
我的建议是查看NSTimeZone
的Apple文档。
如果你这样做,你就会意识到有一种方法-secondsFromGMT
从格林威治时间返回秒。
也许你也可以创建自己的date formatter字符串作为-dateFormat
的NSDateFormatter
属性传递并直接获得时区的字符串表示。像@"ZZZZZ"
那样的东西。
单击here获取有关如何正确编码日期格式的信息。
试试这个:
NSDate *myDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
[dateFormatter setTimeStyle:NSDateFormatterLongStyle];
[dateFormatter setDateFormat:@"ZZZ"];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName: @"Asia/Kolkata"];
[dateFormatter setTimeZone:timeZone];
NSString *dateString = [dateFormatter stringFromDate: myDate];
NSLog(@"%@", dateString);
OUTPUT:
+0530
有关更多信息,请阅读@Andrea答案
-(void)getCurrentTimeZoneMins
{
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
float timeZoneOffset = [destinationTimeZone secondsFromGMTForDate:[NSDate date]] / 60;;
NSLog(@"%f mins",timeZoneOffset);
}
-(void)printTimeZone
{
NSDateFormatter *localTimeZoneFormatter = [NSDateFormatter new];
localTimeZoneFormatter.timeZone = [NSTimeZone localTimeZone];
localTimeZoneFormatter.dateFormat = @"Z";
NSString *localTimeZoneOffset = [localTimeZoneFormatter stringFromDate:[NSDate date]];
NSLog(@"%@",localTimeZoneOffset);
}