-2

I am trying to work out the number of seconds between two times.

My proposed method is to get the Unix times for ' Date 1' and 'Date 2' then deduct them to get the number of seconds.

The second date will be in the format of a string.

How do I convert a string into a unix time in swift?

Any help Much appreciated!

Rashwan L
  • 38,237
  • 7
  • 103
  • 107
MattBlack
  • 3,616
  • 7
  • 32
  • 58

2 Answers2

0

Use NSDate and the method: timeIntervalSinceDate which returns a NSTimeInterval which is a Double value in seconds.

NSDateFormatter method dateFromString will covert a date expressed as a string into an NSDate. See: Date Field SymbolTable. for date/time formats.

Try writing the code, add it to the question if you have problems.

zaph
  • 111,848
  • 21
  • 189
  • 228
0

You could use something like this:

// Your string date
let str = "2016-10-10 12:01:00"
// Create a new DateFormatter
let dateFormatter = DateFormatter()
// Set the dateFormat
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
// Create a new date object from your string
let date = dateFormatter.date(from: str)
// get the unix value
let timestamp = date?.timeIntervalSince1970
Rashwan L
  • 38,237
  • 7
  • 103
  • 107