Convert string array to datetime

5 visualizaciones (últimos 30 días)
Liam Quantrill
Liam Quantrill el 31 de Jul. de 2019
Comentada: Liam Quantrill el 31 de Jul. de 2019
I am trying to convert a string in the format seen below into a datetime:
"2016-07-22 10:02:54.087216500-04:00"
I have had a look at the MATLAB documentation and tried the following:
datetime(ans, 'InputFormat', 'yyyy-MM-dd''T''HH:mm:ss.SSSSSSSSSXXX', 'TimeZone', 'America/New_York')
I receive the following error however:
"Unable to convert the text to datetime using the format 'yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSSXXX'."
I am not actually sure that the timezone is New York, as I pulled the data from the internet and there was no indication of this. It was the only one in the list found in the MATLAB help under the "TimeZone" section of the "datetime" help that had -04:00 however, so I assumed that it would be this.
Can someone see where my mistake in understanding the format of the date is? If so please could you provide a solution that will help me read this format of string into a datetime?
Thanks!

Respuesta aceptada

Guillaume
Guillaume el 31 de Jul. de 2019
Yes, there's no 'T' in your input so the format should be 'yyyy-MM-dd HH:mm:ss.SSSSSSSSSXXX'
>> s = "2016-07-22 10:02:54.087216500-04:00";
>> datetime(s, 'InputFormat', 'yyyy-MM-dd HH:mm:ss.SSSSSSSSSXXX', 'TimeZone', 'America/New_York', 'Format', 'dd MMM yyyy HH:mm:ss z')
ans =
datetime
22 Jul 2016 10:02:54 EDT
I'm overriding the display format in the above. This is of course optional.
  1 comentario
Liam Quantrill
Liam Quantrill el 31 de Jul. de 2019
Ah ok, my bad for not spotting that... Thanks for your help!

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.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by