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

417
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 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