如何在 Swift 中检查 24 小时是否已经过去 [英] How to check if 24 hours have passed in Swift

查看:26
本文介绍了如何在 Swift 中检查 24 小时是否已经过去的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在我的应用程序中实现,在用户保存他的录音后,我必须确定自该录音的创建日期起是否已经过去了 24 小时.到目前为止,我目前所拥有的只是确定当前日期是否不等于创建日期.我真的很感谢任何人的帮助,提前致谢.

I have to implement in my app where after a user had saved his recording, I will have to determine if 24 hours have passed from the creation date of that recording. So far what I have at the moment is just to determine if current date is not equal to the creation date. I really appreciate anybody's help, thanks in advance.

推荐答案

您可以使用 UserDefault 在创建记录时保存日期.语法为

You can use UserDefault to save the date upon creation of the record. The syntax will be

UserDefaults.standard.set(Date(), forKey:"creationTime")

每当你想查看保存的日期时,就用这种方式找回

Whenever you want to check the saved date, retrieve it in this way

if let date = UserDefaults.standard.object(forKey: "creationTime") as? Date {
    if let diff = Calendar.current.dateComponents([.hour], from: date, to: Date()).hour, diff > 24 {
        //do something
    }
}

这篇关于如何在 Swift 中检查 24 小时是否已经过去的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆