Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

564
Vistas
How to find the difference in minutes between two unix time in swift?

I need to find the difference between two Unix epoch time.

let currentTime = Date().timeIntervalSince1970
let comingFromTheServer = TimeInterval(1552078800000)

How to find the difference in minutes between two time? Both are Epoch time in local time zones

Edit

let currentTime = Date().timeIntervalSince1970  // printing 25842307795.623497 which is somewhere in january
let startedTinme = TimeInterval(1552090800000 / 1000)

print((currentTime - startedTinme) / 60) 

let formatter = DateComponentsFormatter()
formatter.allowedUnits = [.minute, .second]
formatter.unitsStyle = .abbreviated
let formatted = formatter.string(for: currentTime - startedTinme)
print(formatted)

Edit 2

I guess there is some miscommunication happened. I want to find out the difference between current time vs specific time. I tried (updated) version of the code:

I am using this extension to get the current time in milliseconds (Epoch time)

extension Date {
var millisecondsSince1970:Int {
    return Int((self.timeIntervalSince1970 * 1000.0).rounded())
}

init(milliseconds:Int) {
    self = Date(timeIntervalSince1970: TimeInterval(milliseconds) / 1000)
}
}

I am receiving the time from the server i.e. 1552086180000 which is 5:03 PM

My current time is 5:12 PM. So the difference should be 9 Minutes. To do so, I am using

let currentTime = Date().millisecondsSince1970
let startedTinme = TimeInterval(1552086180000) / 1000
How can I get the difference of `9` minutes here ?
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

First of all, you have to convert the time coming from server to TimeInterval correctly. Unix time is usually in milliseconds while TimeInterval is in seconds, therefore:

let currentTime = Date().timeIntervalSince1970
let comingFromTheServer = TimeInterval(1552078800000) / 1000

Then just calculate difference and convert to minutes:

print((currentTime - comingFromTheServer) / 60) // 160

or formatted:

let formatter = DateComponentsFormatter()
formatter.allowedUnits = [.minute, .second]
formatter.unitsStyle = .abbreviated
let formatted = formatter.string(from: currentTime - comingFromTheServer) ?? ""
print(formatted) // 160m 41s

Considering your millisecondsSince1970 function, just convert it back to seconds:

let currentTime = TimeInterval(Date().millisecondsSince1970) / 1000
let startedTime = TimeInterval(1552086180000) / 1000
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda