CRey
CRey

Reputation: 1160

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!

Upvotes: 4

Views: 2548

Answers (2)

thevikasnayak
thevikasnayak

Reputation: 629

Including swift 5+ **Pass date like yourDatePicker.date **

 func getTimeStampFromDate(date: Date) -> String {
    return String(format: "%.0f", (date.timeIntervalSince1970 * 1000))
}

Upvotes: 1

rmaddy
rmaddy

Reputation: 318824

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.

Upvotes: 11

Related Questions