1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import Foundation // Obtener la fecha actual let fechaActual = Date() // Formatear la fecha como string let formatter = DateFormatter() formatter.dateFormat = "dd/MM/yyyy HH:mm:ss" let fechaString = formatter.string(from: fechaActual) print("Fecha actual: \(fechaString)") // Crear una fecha a partir de un string let dateString = "22/12/2023 14:30:00" if let fechaDesdeString = formatter.date(from: dateString) { print("Fecha desde string: \(fechaDesdeString)") } // Operaciones con fechas let calendar = Calendar.current if let tomorrow = calendar.date(byAdding: .day, value: 1, to: fechaActual) { print("Mañana será \(formatter.string(from: tomorrow))") } // Obtener componentes de una fecha let components = calendar.dateComponents([.year, .month, .day], from: fechaActual) if let year = components.year, let month = components.month, let day = components.day { print("Año: \(year), Mes: \(month), Día: \(day)") } |
