如何从 iDevices 获取 24 小时格式时间?
我正在开发需要 iPhone 日期和时间才能在服务器上发布的应用程序。现在,根据地区和时区,世界各地不同设备上的日期和时间总是不同的。我希望只有 24 小时内的本地日期时间,但如果用户更改了 setting>date & time>24 hour time>off
由于我没有获得“yyyy-MM-dd HH:mm:ss”格式的时间,它在服务器端产生问题并在服务器上显示错误的日期和时间。对此有什么非常具体的解决方案吗?
I am working on application that required iPhone date and time to post on server. now this date and time on different devices are always different all over the world based on region and time zone. I would like to have local date time in 24 hrs only, though if the user has changed the setting>date & time>24 hour time>off
As I am not getting the time in 'yyyy-MM-dd HH:mm:ss' format, it is creating issue on sever side and showing wrong date and time on server. Is there any very specific solution for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我建议您应该避免将格式化日期发送到服务器,而改为发送 UNIX 时间戳。
[[NSDate date] timeIntervalSince1970]
为您提供一个与时区无关的时间戳,您无需解析该时间戳。如果您还需要时区,请将其标识符作为附加参数发送。I'd suggest you should avoid sending formatted dates to a server and send UNIX timestamps instead.
[[NSDate date] timeIntervalSince1970]
gives you a time zone-independent timestamp which you don't have to parse. If you need the time zone too, send its identifier as an additional parameter.如果您需要 24 小时格式的时间,请在 dateFormatter 中使用大写 H:
If you need time in 24 hour format then use capital H in dateFormatter:
就像 Costique 所说,使用 UNIX 时间戳,但如果必须采用字符串格式,在格式字符串中包含时区并在服务器上处理它,则需要类似以下内容:
Like Costique says, use an UNIX timestamp, but if you must have it in a String format, include the time zone in the format string and handle it on the server, you need something like: