将GMT时间转换为当地时间 [英] Convert GMT time to local time

查看:470
本文介绍了将GMT时间转换为当地时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前的GMT时间 - 2013-05-15 08:55 AM

I got current GMT time - 2013-05-15 08:55 AM

我当前的当地时间是 - 2013 -05-15 02:06 PM
和时区是 - 亚洲/加尔各答

my current local time is - 2013-05-15 02:06 PM and time zone is - Asia/Kolkata

我试图转换上面的格林尼治标准时间到我当地时间这个代码

I tried to convert the above GMT to my local time with this code

   NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"Asia/Kolkata"];
   [dateFormatter setTimeZone:sourceTimeZone];


      // timeStamp - is the above GMT time

   NSDate *dateFromString = [dateFormatter dateFromString:timeStamp];
    NSLog(@"local time is %@",dateFromString);

但这给了我wron时间 - 2013-05-14 19:04:00 +0000 而不是 2013-05-14 02:06 PM

but this give me wron time as - 2013-05-14 19:04:00 +0000 instead of 2013-05-14 02:06 PM

为什么会这样?
请帮我清除。谢谢提前

Why this happens? please help me to clear this.Thanks in advance

推荐答案

没有什么不对, NSDate 实例永远不会有时区(好吧,但它总是GMT,由日期描述中的 +0000 时区表示)。

Nothing is going wrong, a NSDate instance will never have a timezone ( well it will but it is always GMT, indicated by the +0000 timezone in the date description).

您在代码中告诉dateformatter的是您要解析的日期的时区是 Asia / Kolkata 但你希望它是 GMT

What you are telling dateformatter in your code is that the timezone of the date you want to parse is Asia/Kolkata but you want it to be GMT.

首先你要做一个 NSDate 来自输入日期字符串的对象,时区设置为正确的时区, GMT 如您所示。

First you need to make a NSDate object from the input date string with the time zone set to the correct time zone, GMT as you indicated.

NSTimeZone *gmtTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
[dateFormatter setTimeZone:sourceTimeZone];
NSDate *dateFromString = [dateFormatter dateFromString:timeStamp];
NSLog(@"Input date as GMT: %@",dateFromString);

然后当您将日期作为字符串使用时:

Then when you present the date as an string use :

NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithName:@"Asia/Kolkata"];
[dateFormatter setTimeZone:sourceTimeZone];
NSString *dateRepresentation = [dateFormatter stringFromDate:dateFromString];
NSLog(@"Date formated with local time zone: %@",dateRepresentation);

这篇关于将GMT时间转换为当地时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆