Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

409
Views
Cómo obtener la fecha de hoy y mañana en swift 4

¿Cómo obtener la fecha actual en unix-epoch ? timeIntervalSince1970 imprime la hora actual. ¿Hay alguna manera de obtener la hora de hoy a las 12 AM?

Por ejemplo, la hora actual es: 7 de enero de 2018 a las 5:30 p. m. timeIntervalSince1970 imprimirá la hora actual, es decir 1546903800000 . La fecha actual en el sistema de época será el 7 de enero de 2018 a las 00:00 a. m. es decir 1546848000000

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Esto se puede hacer de manera muy simple usando el siguiente código. No hay necesidad de componentes de fecha u otras complicaciones.

 var calendar = Calendar.current // Use the following line if you want midnight UTC instead of local time //calendar.timeZone = TimeZone(secondsFromGMT: 0) let today = Date() let midnight = calendar.startOfDay(for: today) let tomorrow = calendar.date(byAdding: .day, value: 1, to: midnight)! let midnightEpoch = midnight.timeIntervalSince1970 let tomorrowEpoch = tomorrow.timeIntervalSince1970
over 4 years ago · Santiago Trujillo Report

0

Yo haría esto con componentes.

Suponiendo que necesita tiempo en segundos según lo definido por time(2) . Si necesita en milisegundos como lo define time(3) , puede multiplicarlo por 1000.

 // Get right now as it's `DateComponents`. let now = Calendar.current.dateComponents(in: .current, from: Date()) // Create the start of the day in `DateComponents` by leaving off the time. let today = DateComponents(year: now.year, month: now.month, day: now.day) let dateToday = Calendar.current.date(from: today)! print(dateToday.timeIntervalSince1970) // Add 1 to the day to get tomorrow. // Don't worry about month and year wraps, the API handles that. let tomorrow = DateComponents(year: now.year, month: now.month, day: now.day! + 1) let dateTomorrow = Calendar.current.date(from: tomorrow)! print(dateTomorrow.timeIntervalSince1970)

Puedes obtener ayer restando 1.


Si necesita esto en la hora universal (UTC, GMT, Z... cualquiera que sea el nombre que le dé a la hora universal), use lo siguiente.

 let utc = TimeZone(abbreviation: "UTC")! let now = Calendar.current.dateComponents(in: utc, from: Date())
over 4 years ago · Santiago Trujillo Report

0

También intente agregar el siguiente código en la extensión de fecha:

 extension Date { var startOfDay: Date { return Calendar.current.startOfDay(for: self) } func getDate(dayDifference: Int) -> Date { var components = DateComponents() components.day = dayDifference return Calendar.current.date(byAdding: components, to:startOfDay)! } }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!