iPhone - NSTimer - initWithFireDate 并传递本地时间
在我的应用程序中,我使用 NSTimer 并使用 initWithFireDate。 我想从现在开始一小时后启动计时器。 “现在”是指当地时区。 每当我尝试添加一小时并尝试打印它时,它都会打印 GMT 时间,而不是根据我的时区。 我该如何修复这个错误? 我想要从现在起 1 小时后的 NSDate,具体取决于当地时区。
In my app, I'm using NSTimer and using initWithFireDate.
I want to fire the timer after one hour from now. "Now" means local time zone.
When ever I'm trying to get the add add one hour to it and trying to print it, its printing the time in GMT and not according to my timezone.
How can I fix this bug?
I want NSDate with 1 hour from now depending on local time zone.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
此代码将帮助您获取当地时间,您可以相应地从那里向前推进:
This code would help you get the local time and you can take it forward from there accordingly:
NSDateFormatter *formater=[[NSDateFormatter alloc] init];
formater.timeZone=[NSTimeZone localTimeZone];
使用它来本地化您的时区。
NSDateFormatter *formater=[[NSDateFormatter alloc] init];
formater.timeZone=[NSTimeZone localTimeZone];
use this for to localized your time zone.
您可以使用 NSDateFormatter 采取不同格式的日期字符串来做到这一点。只要尝试一下,您就会得到您期望的答案。
You can do it this with NSDateFormatter taking Different Formats of the Date String. Just try it out , You will ge your expected answer.
获取时间后,找到时区并相应添加时差。
After getting the time, find the time zone and add the time difference accordingly.
这是我用 Swift 编写的 Bikramjit Singh 代码的版本(仅适用于我使用“UTC”作为缩写)。希望它有帮助:
更新
我实际上找到了一种更短的获取日期的方法,这种方法对我来说效果更好:
This is my version of Bikramjit Singh's code in Swift (only worked for me by using "UTC" as the abbreviation). Hope it helps:
UPDATE
I actually found a shorter way to get the date, this way has worked better for me: