I'm querying the createdAt
date from several objects in Parse. I want to create a 24 hour (or 48/72, etc.) countDown timer that counts down from when the object was created to 24 hours later. (I'm also then formatting it, and displaying it on a cell UILabel.)
Example: If an object was created at 19:34:33, I want it to expire 24 hours after that (or how ever many hours i specify after it was created). Ultimately showing on the UILabel the hours left until the object expires.
Currently, I'm retrieving when it was created making it repetitively count down from when it was created.
However, I want to make the logic so that it takes when the object was created and then shows how many hours are left until the 24 hours or 48 hours, 72 hours, etc are up.
EDIT
Thanks to @pulsar I added a few more lines of code to the description below. The problem now is that I can only retrieve and correctly countDown the createdAt date and time for 1 object, because only the first object is queried, making all the other objects have the same expiration countDown timer in their respective indexPath.row as the first object in Parse.
I can't figure out how to add all the objects so that they all have their own respective countDown expiration times that is triggered by the expiresAt function.
Here's how i'm querying it and formatting it (in the viewDidLoad): This is the question I asked that help me format the dates: Swift countDown timer Logic
Please see the comments in the code!
var createdAt = object.createdAt
if createdAt != nil {
//assuming this is where i have to set expiration logic?
let calendar = NSCalendar.currentCalendar()
let comps = calendar.components([.Hour, .Minute, .Second], fromDate: createdAt as NSDate!)
let hours = comps.hour * 3600
let minutes = comps.minute * 60
let seconds = comps.second
//I'm adding these two lines below but not sure what to do with them considering I need to add all the objects to an array that will then display it on indexPath.row(s)
let twentyFourHours = NSTimeInterval(60 * 60 * 24)
self.expiresAt = NSDate(timeInterval: twentyFourHours, sinceDate: object.createdAt!!)
self.timerInt.append(hours + minutes + seconds)
//What do i append to the timerInt array? How can i append the objects while triggering the the expiresAt function?
}
Here's my countDown function:
func countDown() {
//timerInt is an array where I'm storing the Int values.
for i in 0 ..< timerInt.count {
let hours = timerInt[i] / 3600
//have to somehow add the expiresAt method while looping through each value [i]...?
let minsec = timerInt[i] % 3600
let minutes = minsec / 60
let seconds = minsec % 60
print(String(format: "%02d:%02d:%02d", hours, minutes, seconds))
timerInt[i]--
//Im assuming best practice would be to loop through the values in order to change the values/set the expiration time to each object (the expiresAt method). Any ideas of how and where I can add this in this loop so that it reflects the countDown I want to set?
}
self.tableView.reloadData()
}
Lastly, for my indexPath.row, I am formatting it and displaying it like this:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let myCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! cell
//I'm formatting the hours, minutes, seconds. However I'm missing the expiresAt function and I have no clue as to where and how to include it... Should it be here or in the countDown() loop?
let hours = timerInt[indexPath.row] / 3600
let minsec = timerInt[indexPath.row] % 3600
let minutes = minsec / 60
let seconds = minsec % 60
myCell.secondLabel.text = String(format: "%02d:%02d:%02d", hours, minutes, seconds)
return myCell
}
Ideas on how to set it to countdown 24/48/72 hours later from when it was created?
Any and all help is much appreciated!