convert a string into a date and time format

1 visualización (últimos 30 días)
Jorge Luis Paredes Estacio
Jorge Luis Paredes Estacio el 4 de En. de 2023
Comentada: Star Strider el 11 de Mzo. de 2025
Hello, How can I convert this string into a date and time format in matlab. The months will be in spanish and the number of letters may vary for the month. This data will be obtained from different files and it will vary.
m ='October17,196616:41:00'
The result should be date_time= 1996/10/17 16:41:00
Thank you

Respuesta aceptada

Star Strider
Star Strider el 4 de En. de 2023
Try something like this —
m ='October17,196616:41:00';
DT = datetime(m, 'InputFormat','MMMMdd,yyyyHH:mm:ss', 'Format','yyyy/MM/dd HH:mm:ss')
DT = datetime
1966/10/17 16:41:00
.
  4 comentarios
Steven Lord
Steven Lord el 11 de Mzo. de 2025
If you've imported the data as a string array:
s1 = ["11-Mrz-2025 10:03:47"; "12-Dec-2025 09:22:38"]
s1 = 2x1 string array
"11-Mrz-2025 10:03:47" "12-Dec-2025 09:22:38"
s2 = replace(s1, ["Mrz", "Dec"], ["März", "Dez"])
s2 = 2x1 string array
"11-März-2025 10:03:47" "12-Dez-2025 09:22:38"
d2 = datetime(s2, Locale="de_DE")
d2 = 2x1 datetime array
11-Mar-2025 10:03:47 12-Dec-2025 09:22:38
You could write a small helper function, call it standardizeMonth, to perform that replace call to replace any non-standard abbreviations you use.
Star Strider
Star Strider el 11 de Mzo. de 2025
@Jim Benjamin — You don’t have to rename all of them, only the non-standard ones.
Also, as I suggested and that @Steven Lord provided an example for, renaming the ones that don’t match MATLAB’s set (see my earlier Comment) can be done in one line using the strrep function.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Dates and Time en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by