3

I am not sure what I am doing wrong, I need to find difference between two dates and extract seconds from it, below is my code. I am not getting correct seconds. There is difference of seconds.

  public func captureStartTime() {
    captureStartDateTime = Date()
  }

  public func captureEndTime(eventType: String, eventElement: String) {
    let difference = Date().timeIntervalSince(captureStartDateTime)
    let interval = Int(difference)
    let seconds = interval % 60
    let secondsDescrp = String(format: "%02d", seconds)
}
Kashif Jilani
  • 1,207
  • 3
  • 22
  • 49
  • 2
    Possible duplicate of [Getting the difference between two NSDates in (months/days/hours/minutes/seconds)](https://stackoverflow.com/questions/27182023/getting-the-difference-between-two-nsdates-in-months-days-hours-minutes-seconds) – Rocky Mar 06 '19 at 04:43
  • 1
    Date.timeIntervalSince(_:) will give you the interval in seconds. By the % 60 operation, are you trying to convert this to minutes and seconds? – Chris Shaw Mar 06 '19 at 04:43
  • As your code stands `difference` is the number of seconds between now and `captureStartDateTime` – MadProgrammer Mar 06 '19 at 04:43

3 Answers3

4

interval is the answer you want. That is the total number of seconds between the two dates.

Your seconds value would only be useful if you wanted to calculate the number of hours, minutes, and seconds or the number of minutes and seconds from the total number of seconds.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Thanks for sharing, however still interval is not giving exact second values. There is difference if 10 - 15 seconds between previous and current date. I have also used NSCalendar still its not giving accurate one. – Kashif Jilani Mar 06 '19 at 04:55
  • You need to update your question with more specific details. Show the exact values being used and your actual and expected results. – rmaddy Mar 06 '19 at 04:56
  • I need to get seconds between two button clicks, so on one button click, i have retrieved date as captureStartDateTime, on second button click i retrieved current date and time and try to find seconds in interval. – Kashif Jilani Mar 06 '19 at 05:02
1

Use the following code to get the difference between two dates, Store current time in startTime when pressed button 1 and store current date time in endTime when pressed button 2, See this code, I hope this helps you.

var startTime:Date!
var endTime:Date!

@IBAction func buttonStartTime(_ sender: UIButton) {
    startTime = Date()
}


@IBAction func buttonEndTime(_ sender: UIButton) {
    endTime = Date()

    let formatter = DateComponentsFormatter()
    formatter.allowedUnits = [.second]
    formatter.unitsStyle = .full
    let difference = formatter.string(from: startTime, to: endTime)!
    print(difference)//output "8 seconds"
}

Output

8 seconds

AtulParmar
  • 4,358
  • 1
  • 24
  • 45
0

you can also use default date components and according to that compare your dates and you can get the difference in year, month, day etc

let dateString1 =  "2019-03-07T14:20:20.000Z"

let dateString2 =  "2019-03-07T14:20:40.000Z"



//set date formate

let Dateformatter = DateFormatter()

Dateformatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"



//convert string to date

let dateold = Dateformatter.date(from: dateString1)!

 let datenew = Dateformatter.date(from: dateString2)!



 //use default datecomponents with two dates

 let calendar1 = Calendar.current

 let components = calendar1.dateComponents([.year,.month,.day,.hour,.minute,.second], from:  dateold, to:   datenew)



 let seconds = components.second

 print("Seconds: \(seconds)")