如何在Swift3中从DateFormatter()获取Int [英] How to get Int from DateFormatter() in Swift3

查看:49
本文介绍了如何在Swift3中从DateFormatter()获取Int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当数据传递24小时后,我想从我的 UITableView 中删除一些数据.我将日期另存为String.我想检查日期,但是因为它是 String ,所以我如何才能将该数据转换为 Int 值,以便检查自从最后输入?有什么好的代码可以实现吗?此日期数据存储在Firebase中.

I want to remove some data from my UITableView when the data is passed over 24 hours. I saved date as String. I want to check the date but because it is String, how am I able to turn that data into Int value in order to check how many hours have passed since the data is last entered? Is there any good code for that to happen? This date data is stored in Firebase.

 let currentDateTime = Date()
 let formatter = DateFormatter()
 formatter.timeStyle = .medium
 formatter.dateStyle = .long
 let date = formatter.string(from: currentDateTime)

推荐答案

您需要:

  1. 您的日期
  2. 在该日期之前增加24小时
  3. 将新日期与现在"进行比较

我假设您已经有某个约会:

I am assuming you have some date already:

let myDate: Date = ...

让我们增加24小时:

let endDate: Date = Calendar.current.date(byAdding: .hour, value: 24, to: myDate, wrappingComponents: true)!

我约会以来已经过了24小时吗?

Did 24 hours already passed since my date?

let now = Date()
let expired = (endDate < now)

我不确定您为什么还要为此任务使用日期格式化程序.

I am not sure why would you even use a date formatter for this task.

这篇关于如何在Swift3中从DateFormatter()获取Int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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