Skip to content

Instantly share code, notes, and snippets.

@fitomad
Last active May 2, 2022 13:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fitomad/30e40bb75ed764bf5ddef1c78a516a69 to your computer and use it in GitHub Desktop.
Save fitomad/30e40bb75ed764bf5ddef1c78a516a69 to your computer and use it in GitHub Desktop.
/**
Convierte un String a Date con el formato
de fecha indicando en el archivo CSV
Por ejemplo: `01/01/2021 0:00`
*/
func dateTimeFromFrame(string: String) -> Date?
{
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd/MM/yyyy HH:mm"
return dateFormatter.date(from: string)
}
/**
Convierte un String a Date con el formato
de fecha indicando en el archivo CSV
Por ejemplo: `01/01/2021`
*/
func dateFromFrame(string: String) -> Date?
{
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd/MM/yyyy"
return dateFormatter.date(from: string)
}
// Establecemos las opciones de lectura del archivo CSV
var csvReadOptions = CSVReadingOptions(hasHeaderRow: true, delimiter: ";")
// Si indicamos que el tipo de datos es Date entonces
// parsearlo mediante nuestro closure
csvReadOptions.dateParsers = [
dateTimeFromFrame(string:),
dateFromFrame(string:)
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment