将GMT时间转换为当地时间

编程入门 行业动态 更新时间:2024-10-28 15:21:07
本文介绍了将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时间转换为当地时间

本文发布于:2023-10-17 13:28:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1501066.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   当地时间   时间   GMT

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!