简体   繁体   中英

Compare two dates for Countdown Timer Swift2

I'm querying the createdAt column from Parse.

My Parse methods are above this code and then I'm doing this:

var createdAt = object.createdAt

if createdAt != nil {

    let twentyFourHours = NSTimeInterval(60 * 60 * 24)
    self.expiresAt = NSDate(timeInterval: twentyFourHours, sinceDate: (createdAt!!))

}

I am querying many dates from Parse. However, I'm unable to store them in createdAt because createdAt is of type NSDate? .

I need to make an array, but I can't figure out how to store many NSDate values so that I can compare them.

How can I store the values I'm querying and put them into an array so that I can compare many createdAt dates with the NSTimeInterval method using this method: NSDate(timeInterval: twentyFourHours, sinceDate: (createdAt!!)) ?

Am I using the correct function?

Thanks in advance.

I guess the easiest way is to store the data as Double

let date = NSDate().timeIntervalSince1970
let doubleDate = Double(date)

Create an array, store and very easy to compare. Than when you query it, just do:

var date = NSDate(timeIntervalSince1970: Double(dateDouble))

exemple:

    func exemple() {
    // SAVING
    // take you date
var array = [Double]()

    let dateToSave = NSDate().timeIntervalSince1970
    let dateToSave1 = NSDate().timeIntervalSince1970
    let dateToSave2 = NSDate().timeIntervalSince1970
    let dateToSave3 = NSDate().timeIntervalSince1970
    let dateToSave4 = NSDate().timeIntervalSince1970

    array.append(Double(dateToSave))
    array.append(Double(dateToSave1))
    array.append(Double(dateToSave2))
    array.append(Double(dateToSave3))
    array.append(Double(dateToSave4))
    print(array)
    let objectToSave = PFObject(className:"SomeDates")
    objectToSave["dates"] = array
    objectToSave.saveInBackgroundWithBlock {
        (success: Bool, error: NSError?) -> Void in
        if (success) {
            //done


        } else {
            // done != true :))
        }
    }
}

create a column of type array

i test it and it works!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM