Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

562
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda