Search code examples
iosswiftswift3

Converting UIDatePicker result to unix timestamp


My issue is trying to convert a Date Picker result into a unix timestamp. My client is adamant that it'd be saved as a unix timestamp into Firebase database. Im also quite new to the swift so...

let myTimeStamp = NSDate(timeIntervalSince1970: self.datePicker?.date)

this is the result of datePicker: 2016-12-03 00:56:00 +0000 This is the error: Cannot convert value of type 'Date?' to expected argument type 'TimeInterval' (aka 'Double')

Please help sirs & ma'ams!


Solution

  • You already have the Date. Since you want the timestamp, call:

    let myTimeStamp = self.datePicker?.date.timeIntervalSince1970
    

    myTimeStamp will be an NSTimeInterval with a value in Unix time.