gpt4 book ai didi

ios - 查找自上次用户更新 iOS 以来的时间

转载 作者:行者123 更新时间:2023-11-28 20:39:19 24 4
gpt4 key购买 nike

我正在构建一个 iOS 应用程序,用户可以在该应用程序中输入进入本地酒吧/餐厅的预计等待时间。为了增加可靠性,我希望能够跟踪用户何时输入信息,以及那个时间到一个 xml 文件,然后能够将该文件解析为 TableView (与之前的更新一起)以供其他人查看。表格单元格将显示位置名称、大致等待时间以及输入等待时间的时间。

本质上,我想弄清楚的是如何在用户更改 cel 时格式化 NSDate(我希望它看起来像一个时间,例如晚上 10:30)。给定一个时间,我如何找到当前时间和给定时间的差异?

谢谢

最佳答案

那里有几个问题,没有一个真正符合标题,但这里是 ;-)

获取当前时间/日期:

NSDate *now = [NSDate date];

为了保存它,我建议你将它存储在 NSUserDefaults 中:

[[NSUserDefaults standardUserDefaults] setObject:now forKey:@"startTime"];
[[NSUserDefaults standardUserDefaults] synchronize];

您可以稍后使用

再次加载日期
NSDate *theDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"startTime"];

您可以使用 NSDateFormatter 将日期格式化为人类可读的内容,如下所示:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"HH:mm"];
NSString *time = [formatter stringFromDate:theDate]; // returns something like @"10:30"

您可以使用如下函数找出两个日期之间的差异:

NSTimeInterval differenceInSeconds = [date1 timeIntervalSinceDate:date2];

时间间隔以秒为单位,因此要将其转换为有用的时间,您可以除以 60 得到分钟,再除以 60 得到小时等。所以要将其转换为人类可读的时间:

NSTimeInterval differenceInSeconds = ...

NSInteger totalSeconds = floor(differenceInSeconds);
NSInteger seconds = totalSeconds % 60;
NSInteger minutes = totalSeconds / 60;
NSString *waitTime = [NSString strinWithFormat:@"%i minutes, %i seconds", minutes, seconds];

关于ios - 查找自上次用户更新 iOS 以来的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9237033/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com