更改当前日期
我正在 iPhone 开发中使用日期。我需要将日期从当前日期增加 24 小时。
I am working with the date in my iPhone development. I need to increase the date by 24 hours from the current date.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以使用 dateByAddingTimeInterval
You can use dateByAddingTimeInterval
简单的:
Simple:
您可以添加 NSTimeInterval。由于这是以秒为单位的双倍,只需添加 24*60*60:
you can add an NSTimeInterval. Since this is a double in seconds just add 24*60*60:
尝试
Try
将下载日期保存在 NSUserdefaults 中,然后检查是否已经过了 24 小时?
如果没有通知用户他/她将不得不等待。
Save the download date in the NSUserdefaults then check if 24 hours have passed?
If not notify the user that he/she will have to wait.
将上次下载日期保存在 NSUserDefaults。当您尝试下载时,只需检查该日期即可。
像这样的事情:
您可以在适当的地方调用
[self downloadDataForecedUpdate:NO]
。在应用程序启动时或在 IBAction 中。使用该方法的返回值来显示下载指示器或警告用户必须等待更多时间。save the last download date in NSUserDefaults. And when you try to download just check for that date.
something like this:
you can call
[self downloadDataForecedUpdate:NO]
whereever it is appropriate. on application launch or in an IBAction. Use the return value of the method to either show a download indicator or a alert that tells the user he has to wait more.根据您真正想要实现的目标,仅在时间间隔中添加 86,400 (或 60 * 60 * 24 )可能不是您想要的。例如,如果用户经历夏令时调整,那么您将关闭一个小时,如果发生在午夜附近,甚至会关闭一天。更好的方法是向 NSCalendar 询问结果,该结果考虑了用户本地时区。
Depending on what you are really trying to achieve, just adding 86,400 (or 60 * 60 * 24 ) to your time interval might not be what you want. If for example, the user experiences a daylight-savings adjustment, then you will be off by an hour, or even a day if it happens near midnight. A better method is to ask the NSCalendar for the result, which takes into account the users local time zone.