2

I get UTC time as a string.Now i want to convert this string UTC time into unix time stamp. Please suggest how can I convert UTC time to unix time stamp.

Nirav D
  • 71,513
  • 12
  • 161
  • 183
TechChain
  • 8,404
  • 29
  • 103
  • 228
  • Here is the (http://stackoverflow.com/a/37856922/3675182) – Sachin Tyagi Dec 26 '16 at 06:34
  • 3
    Does this answer your question? [Swift convert unix time to date and time](https://stackoverflow.com/questions/26849237/swift-convert-unix-time-to-date-and-time) – Ajumal Jan 20 '20 at 07:04

4 Answers4

0

Convert that string to NSDate then use that date with .timeIntervalSince1970, it will produce timestamp in Double

Tj3n
  • 9,837
  • 2
  • 24
  • 35
0

Use this to convert unix time

let timeInterval  = 1415639000 (Your time interval)
let unixTime = NSDate(timeIntervalSince1970: timeInterval)
Lalit kumar
  • 1,797
  • 1
  • 8
  • 14
0

i used it like that

let timeInterval = datePicker.date.timeIntervalSince1970
let unixTime = NSDate(timeIntervalSince1970: timeInterval)
Nihal
  • 5,262
  • 7
  • 23
  • 41
Ibn adam
  • 1
  • 1
-1
extension String {

  //MARK:UTCStringToDate
  var utcStringToUnix:NSTimeInterval {
    let dateFormatter = NSDateFormatter()
    dateFormatter.timeZone = NSTimeZone(name: "UTC")
     dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
    let utcDate = dateFormatter.dateFromString(self)
  let unixTime =   utcDate?.timeIntervalSince1970

       return unixTime!
  }
}

use this extension.. to use just : let unix = utcString.utcStringToUnix

Jagdeep
  • 1,158
  • 11
  • 16