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.
Asked
Active
Viewed 4,738 times
2
-
Here is the (http://stackoverflow.com/a/37856922/3675182) – Sachin Tyagi Dec 26 '16 at 06:34
-
3Does 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 Answers
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
-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