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

查看:161
本文介绍了如何检查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天全站免登陆