首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在iOS中将GMT转换为IST

在iOS中将GMT转换为IST
EN

Stack Overflow用户
提问于 2012-08-04 13:35:13
回答 3查看 4.9K关注 0票数 1

如何在IOS中将GMT转换为IST?我有代码,这将使我的时间在格林尼治标准时间。但我想将其转换为相应的本地时区,并在我的应用程序中使用。我该怎么做呢?提前谢谢。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-08-04 20:07:14

下面的代码将GMT转换为IST。

代码语言:javascript
运行
复制
NSString *inDateStr = @"2000/01/02 03:04:05";
NSString *s = @"yyyy/MM/dd HH:mm:ss";

// about input date(GMT)
NSDateFormatter *inDateFormatter = [[NSDateFormatter alloc] init];
inDateFormatter.dateFormat = s;
inDateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
NSDate *inDate = [inDateFormatter dateFromString:inDateStr];

// about output date(IST)
NSDateFormatter *outDateFormatter = [[NSDateFormatter alloc] init];
outDateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"IST"];
outDateFormatter.dateFormat = s;
NSString *outDateStr = [outDateFormatter stringFromDate:inDate];

// final output
NSLog(@"[in]%@ -> [out]%@", inDateStr, outDateStr);
票数 8
EN

Stack Overflow用户

发布于 2012-08-04 19:09:56

代码语言:javascript
运行
复制
[(NSDateFormatter *) setTimeZone:[NSTimeZone localTimeZone]]; this will give u the local conversion of the GMT to local time zone.
票数 1
EN

Stack Overflow用户

发布于 2016-06-09 22:42:06

我使用下面的代码将GMT转换为IST。

代码语言:javascript
运行
复制
NSTimeZone *zone1=[NSTimeZone localTimeZone];
long second=[zone1 secondsFromGMT]; //offset
NSDate *currentdate=[NSDate date];

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MMddyyyy hh:mm:ss"]; // format
[dateFormat setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:second]];

dateStr = [dateFormat stringFromDate:currentdate];
NSLog(@"Current date %@",dateStr);

这可能会有所帮助。

谢谢

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11806215

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档