如何创建类似世界时钟的应用程序

发布于 2025-01-08 07:46:58 字数 492 浏览 4 评论 0原文

我想创建 iPhone 嵌入式“世界时钟”之类的应用程序。目前,我可以创建一个可以显示当前设备时间的单个时钟(模拟时钟)。

在这里,我用来

NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:[NSDate date]];    

检索当前的日期/时间。基于此 NSDate 对象的模拟时钟逻辑。

问题是,如何将一个国家/地区(澳大利亚、英国...)的日期/时间转换为这种 NSDate 格式?

---------- 而且,当我 NSLog 时(@"%@", [NSDate 日期]);它给出的是 Grin-witch 时间,而不是当前模拟器时间 ---------

I want to create iPhone embedded "World Clock" like application. At the moment I am able to create a single clock (analog clock) which can display Current device time.

Here, I am using

NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:[NSDate date]];    

to retrieve the current Date/Time. Analog clock logic based on this NSDate object.

Problem is, How can I get Date/Time of a Country (Australia, UK ...) on to this NSDate format?

---------- and also, When I NSLog(@"%@", [NSDate date]); it gives Grin-witch time, Not the current Simulator time ---------

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

她说她爱他 2025-01-15 07:46:58

您需要将时间转换为您需要的区域设置,请参阅此答案以了解如何执行此操作,

NSDate 休息 5 小时

You need to convert the time to whichever locale you need, see this answer for how to do that,

NSDate is 5 hours off

望喜 2025-01-15 07:46:58

通过使用 NSCalendar & NSTimeZone,可以获取日期/时间,下面的代码片段展示了如何获取伦敦当前的日期/时间。

let timeZone = NSTimeZone(abbreviation: "BST")
let calendar: NSCalendar! = NSCalendar(identifier: NSCalendarIdentifierGregorian)
let zoneComponents = calendar.componentsInTimeZone(self.timeZone, fromDate: NSDate())
hour = zoneComponents.hour
minute = zoneComponents.minute
second = zoneComponents.second

你可以使用这个名为 SmileClock 的库来实现这一切。

By using the NSCalendar & NSTimeZone, you can get the Date/Time, blew code snippet show how to get the current Date/Time in London.

let timeZone = NSTimeZone(abbreviation: "BST")
let calendar: NSCalendar! = NSCalendar(identifier: NSCalendarIdentifierGregorian)
let zoneComponents = calendar.componentsInTimeZone(self.timeZone, fromDate: NSDate())
hour = zoneComponents.hour
minute = zoneComponents.minute
second = zoneComponents.second

You can use this library called SmileClock achieve all.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文