0

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)
Ryo
  • 83
  • 11

2 Answers2

1

You need:

  1. Your date
  2. Add 24 hours to that date
  3. Compare that new date with "now"

I am assuming you have some date already:

let myDate: Date = ...

Let's add 24 hours:

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

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.

Sulthan
  • 128,090
  • 22
  • 218
  • 270
1

Try this:

 let currentDateTime = Date()
 let formatter = DateFormatter()
 formatter.dateFormat = "hh"
 let date = formatter.string(from: currentDateTime)
 print(date)// Time in string
 print(Int(date)!) // Time in Int
arghtype
  • 4,376
  • 11
  • 45
  • 60
Anil Kumar
  • 945
  • 7
  • 16